【发布时间】:2016-12-30 04:49:02
【问题描述】:
我创建了一个文件 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');
当我尝试在浏览器中访问 /listings/sitemap 时,它会显示一条错误消息:
【问题讨论】:
-
你试过删除缓存吗? debuggable.com/posts/…
-
是的,事实上,当我尝试
print_r($listData );但sitemap.xml 显示为空时,我也可以从数据库中获取数据? -
在您的控制器中设置 listData,但在 sitemap.ctp 中您尝试循环 $listNews ???
-
@Salines 我已将列表新闻更改为列表数据但仍为空白?
-
移动sitemap.ctp到view/listings/xml/sitemap.ctp