Display data from database in sitemap.xml using cakephp 2.0
Asked Answered
W

1

3

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:

image

Wray answered 23/8, 2016 at 11:22 Comment(9)
Have you tried removing the cache? debuggable.com/posts/…Ditter
yes i did in fact i can also get data from database when i try print_r($listData ); but sitemap.xml showing empty?Wray
In your controller you set listData, but in sitemap.ctp you trying to loop $listNews ???Durston
@Durston i have changed list news to listdata but still blank ?Wray
move sitemap.ctp to view/listings/xml/sitemap.ctpDurston
file moved to xml/ but still blank?Wray
debug($listData); $html->link to $this->Html->link() ...Durston
@Durston in my application by default ` $html->link`Wray
please provide solution?Wray
L
0

Put your view in a xml sub folder :

view/listings/xml/sitemap.ctp
Lopes answered 13/8, 2020 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.