2010-09-24 56 views
7

我想创建一个站点地图,但我对Sitemaps的用法知之甚少。我使用CakePHP。谷歌和指南上有很多软件,但我仍然想要问一个简单的方法来为CakePHP创建站点地图。如何为CakePHP创建站点地图?

我上传了网站上的服务器,它不依赖本地主机。

回答

12

这里有一个例子quick'n'dirty你一起玩,并调整您的需求:

在你的控制器:

public $components = array('RequestHandler'); 

public function sitemap() 
{ 
    Configure::write('debug', 0); 

    $articles = $this->Article->getSitemapInformation(); 

    $this->set(compact('articles')); 
    $this->RequestHandler->respondAs('xml'); 
} 

你的 “文章” 的模式:

public function getSitemapInformation() 
{ 
    return $this->find('all', array(/* your query here */)); 
} 

查看:

<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> 
+1

感谢例如 – meotimdihia 2010-09-25 20:48:06

+0

请记住在您的控制器(或AppController.php中为应用程序范围的访问)添加公共'$ components = array('RequestHandler');'以使其正常工作。 – Coreus 2015-11-12 18:55:23

+0

@Coreus我已经更新了答案,谢谢! – 2015-11-13 21:32:54

4

这是一个良好的开端,现在只需加:

Router::parseExtensions('xml');到routes.php文件

从那里,你想有这样的路线:

Router::connect('/sitemap', array('controller' => 'posts' ....., 'ext' => 'xml')),将引导site.com/sitemap.xml到网站地图所在的控制器/操作。

创建具有正确标题的XML布局,移动视图文件的意见/职位/ XML/file.ctp

+0

感谢很多评论 – meotimdihia 2010-09-29 13:53:07

+0

请检查我的问题我做错了,还有sitemap.xml空白文件? http://stackoverflow.com/questions/39099791/display-data-from-database-in-sitemap-xml-using-cakephp-2-0?noredirect=1#comment65552181_39099791 – 2016-08-23 14:43:50

3

更妙的是:添加Router::parseExtensions('xml');到routes.php文件(没有错字)