gpt4 book ai didi

php - 使用 cakephp 2.0 在 sitemap.xml 中显示来自数据库的数据

转载 作者:行者123 更新时间:2023-12-05 07:47:59 24 4
gpt4 key购买 nike

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

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

列表 Controller 文件:

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

站点地图.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 时,它显示一条错误消息:

image

最佳答案

将您的 View 放在 xml 子文件夹中:

view/listings/xml/sitemap.ctp

关于php - 使用 cakephp 2.0 在 sitemap.xml 中显示来自数据库的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com