Category Covered

how to create xml sitemap on cakephp

Here is an simple way to create dynamic sitemap on cakephp.

create a controll named "sitemaps_controller.php" and paste the following code

class SitemapsController extends AppController{
    var $name = 'Sitemaps';
    var $uses = array('Content');
    var $components = array('RequestHandler');
    var $helpers = array('Time'); 
    function index(){
        $this->set('posts', $this->Content->find('all',
array('fields' => array('modified','id','slug'))));
        //debug logs will destroy xml format, make sure were not in drbug mode
        Configure::write ('debug', 0);

 Create a view named "index"

<urlset xmlns="">
    <?php foreach ($posts as $post): ?>
        <loc><?php echo Router::url('/',true); ?>post/
<?php echo $post['Content']['slug'];?></loc>
    <?php endforeach; ?>

  Create a layout named "xmlrss"

<?php header('Content-type: text/xml'); ?>
<?php echo $content_for_layout; ?>


You are done. Change the routes as you want to see the url. Here you will see like

 xml-free sitemap for cakephp.


Comments on "how to create xml sitemap on cakephp"
Comment by, Rahul

Model code are not displaying in your demo code. Please share the model code also. Thanks RJ
Related posts