使用cakephp 2.0在sitemap.xml中显示数据库中的数据

时间:2016-08-23 11:22:46

标签: php cakephp sitemap

我创建了一个文件sitemap.xml并存储在app / webroot /中,这是一个我可以在浏览器中查看的文件,如example.com/sitemap.xml。我创建了sitemap function in controller,我将从数据库中获取数据并传递给view/listings/sitemap.ctp。我还在app / config / routes.php文件中添加了Router::connect

问题是example.com/sitemap.xml文件中没有显示数据吗?

列表控制器文件:

var $name = 'Listings';
var $components = array('RequestHandler');

public function sitemap(){
       $this->layout='ajax'; 
       $this->RequestHandler->respondAs('xml');
       $listData = $this->Listing-
       >find('all',array('conditions'=>array('Listings.status'=>1)
       ,'order'=> array('Listings.created'=>'Desc')));
       $this->set(compact('listData'));
}

Sitemap.ctp文件:

<?php App::uses('CakeTime', 'Utility'); ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
    <loc><?php echo $html->link('/',true); ?></loc>
    <changefreq>weekly</changefreq>
</url>

<?php foreach ($listData as $list){ ?>
<url>
    <loc><?php echo $html->link(array('controller' => 'listings', 'action' 
     => 'sitemap',$list['listings']['id']),true); ?></loc>
    <lastmod><?php echo $this->Time->toAtom($list['listings']['created']); ?
    ></lastmod>
    <changefreq>weekly</changefreq>
</url>
<?php } ?>

</urlset>

Routes.php文件:

Router::connect('/sitemap.xml',array('controller' => 'listings', 
'action' => 'sitemap', 'ext'=>'xml'));
Router::parseExtensions('xml');

当我尝试在浏览器中访问/ listing / sitemap时,它显示错误消息:

image

1 个答案:

答案 0 :(得分:0)

将视图放在xml子文件夹中:

view/listings/xml/sitemap.ctp