facebook twitter hatena line google mixi email

Php/fuelphp/サイトマップ作成

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

サイトマップを作る

  • fuel/app/tasks/makesitemap.php
<?php
namespace Fuel\Tasks;
/**
 * @ex
 * php oil r makesitemap
 */
class Makesitemap
{
        public static function run() {
                $command = new \Model_Command_Makesitemap();
                $command->exec();
    }
}
  • fuel/app/classes/model/command/Makesitemap.php
<?php
class Model_Command_Makesitemap
{
    protected $_domain = 'sample.com';
    public function exec()
    {
        $uris = array(
            '/',
            '/keywordlist',
        );
        $urls = array();
        foreach ($uris as $uri) {
            $urls[] = "http://". $this->_domain . $uri;
        }
        $xml = $this->view($urls);
        File::update(APPPATH . '/../../public/', 'sitemap.xml', $xml);
    }
    public function view($urls)
    {
        $xmlurls = "";
        foreach ($urls as $url) {
$xmlurl = <<<EOD
   <url>
      <loc>$url</loc>
      <changefreq>weekly</changefreq>
      <priority>0.3</priority>
   </url>

EOD;
            $xmlurls .= $xmlurl;
       }
$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
$xmlurls
</urlset>
EOD;
        return $xml;
    }
}

sitemap作成コマンド

上の例だと

php oil r makesitemap