我想创建一个站点地图,但我对Sitemaps的使用知之甚少.我使用CakePHP.谷歌和指南上有很多软件,但我仍然想要问一下,为CakePHP创建站点地图的简单方法.
我在服务器上载了网站,它不依赖于localhost.
dr *_*ter 12
这是一个快速的例子供您玩耍并根据您的需求进行调整:
在你的控制器中:
public $components = array('RequestHandler');
public function sitemap()
{
Configure::write('debug', 0);
$articles = $this->Article->getSitemapInformation();
$this->set(compact('articles'));
$this->RequestHandler->respondAs('xml');
}
Run Code Online (Sandbox Code Playgroud)
你的"文章"模型:
public function getSitemapInformation()
{
return $this->find('all', array(/* your query here */));
}
Run Code Online (Sandbox Code Playgroud)
视图:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($articles as $article): ?>
<url>
<loc><?php echo Router::url(/* generate the URLs here */); ?></loc>
<lastmod><?php echo $time->toAtom(/* last update time here */); ?></lastmod>
<changefreq>weekly</changefreq>
</url>
<?php endforeach; ?>
</urlset>
Run Code Online (Sandbox Code Playgroud)