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:
print_r($listData );
but sitemap.xml showing empty? – Wray