How to create a Sitemap for CakePHP?
Solution 1
Here's a quick'n'dirty example for you to play with and adjust to your needs:
In your controller:
public $components = array('RequestHandler');
public function sitemap()
{
Configure::write('debug', 0);
$articles = $this->Article->getSitemapInformation();
$this->set(compact('articles'));
$this->RequestHandler->respondAs('xml');
}
Your "Article" model:
public function getSitemapInformation()
{
return $this->find('all', array(/* your query here */));
}
View:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($articles as $article): ?>
<url>
<loc><?php echo Router::url(/* generate the URLs here */); ?></loc>
<lastmod><?php echo $time->toAtom(/* last update time here */); ?></lastmod>
<changefreq>weekly</changefreq>
</url>
<?php endforeach; ?>
</urlset>
Solution 2
That is a good start, now just add:
Router::parseExtensions('xml');
to routes.php
From there you want to have a route like:
Router::connect('/sitemap', array('controller' => 'posts' ....., 'ext' => 'xml'))
that will direct site.com/sitemap.xml to the controller/action where the sitemap is.
create a xml layout with the correct headings, and move the view file to views/posts/xml/file.ctp
Solution 3
Even better: add Router::parseExtensions('xml');
to routes.php (without the typo)
Related videos on Youtube
meotimdihia
Updated on May 04, 2022Comments
-
meotimdihia almost 2 years
I want to create a sitemap, but I know very little about the usage of Sitemaps. I use CakePHP. There is a lot software on google and guides, but I still want ask anyway, for an easy way to create sitemaps for CakePHP.
I uploaded the website on the server, it doesn't rely on localhost.
-
Coreus over 8 yearsRemember to add public ´$components = array('RequestHandler');´ in your controller (or AppController.php for app-wide access) for this to work.
-
Admin over 7 yearsplease check my issue where i'm doing wrong and also sitemap.xml blank file? stackoverflow.com/questions/39099791/…
-
Admin over 7 yearsplease check my issue where i'm doing wrong and also sitemap.xml blank file? stackoverflow.com/questions/39099791/…