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

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

 Create a view named "index"

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php foreach ($posts as $post): ?>
    <url>
        <loc><?php echo Router::url('/',true); ?>post/
<?php echo $post['Content']['slug'];?></loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    <?php endforeach; ?>
</urlset>

  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 http://domain.com/sitemaps/index

 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