使用cakephp 2.0在sitemap.xml中显示数据库中的数据 [英] Display data from database in sitemap.xml using cakephp 2.0

查看:52
本文介绍了使用cakephp 2.0在sitemap.xml中显示数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了文件 sitemap.xml 并存储在app / webroot /中,这是我可以从浏览器中查看的文件,例如 .com / sitemap.xml 。我在控制器中创建了 sitemap函数,我将从数据库中获取数据并传递给 view / listings / sitemap.ctp 。另外,我还在app / config / routes.php文件中添加了 Router :: connect

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.

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

Problem is that data is not showing in the example.com/sitemap.xml 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文件:

<?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');

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

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

推荐答案

将视图放入xml子文件夹:

Put your view in a xml sub folder :

view/listings/xml/sitemap.ctp

这篇关于使用cakephp 2.0在sitemap.xml中显示数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆