Display data from database in sitemap.xml using cakephp 2.0

匆匆过客 提交于 2019-12-12 19:22:37

问题


I have created a file sitemap.xml and stored in app/webroot/ and this is a file I can view from browser like this example.com/sitemap.xml. I have created sitemap function in controller where I will get data from database and pass to view/listings/sitemap.ctp. Also I have added Router::connect in app/config/routes.php file.

Problem is that data is not showing in the example.com/sitemap.xml file?

Listings Controller File:

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 File:

<?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 File:

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

When I try to access /listings/sitemap in the browser it shows an error message:

来源:https://stackoverflow.com/questions/39099791/display-data-from-database-in-sitemap-xml-using-cakephp-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!