How to create a Sitemap for CakePHP?
Asked Answered
B

4

7

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.

Bodleian answered 24/9, 2010 at 18:16 Comment(0)
E
14

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>
Endoergic answered 25/9, 2010 at 17:54 Comment(2)
Remember to add public ´$components = array('RequestHandler');´ in your controller (or AppController.php for app-wide access) for this to work.Repulsion
please check my issue where i'm doing wrong and also sitemap.xml blank file? #39100291Tarah
N
4

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

Nolannolana answered 28/9, 2010 at 20:33 Comment(1)
please check my issue where i'm doing wrong and also sitemap.xml blank file? #39100291Tarah
W
3

Even better: add Router::parseExtensions('xml'); to routes.php (without the typo)

Wishywashy answered 23/3, 2012 at 21:4 Comment(0)
O
1

You can follow the following steps:

Step 1. Create a layout file sitemap.ctp for XML sitemap view

<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
echo $this->fetch('content');
echo '</urlset>';
?>

Step 2. Create a separate controller for the Sitemap generation:

class SitemapsController extends AppController
{
    public function index()
    {
        $this->viewBuilder()->setLayout('sitemap');
        $this->RequestHandler->respondAs('xml');
        
        $postTbl = TableRegistry::getTableLocator()->get('Posts');
        $posts = $postTbl->find()->select(['slug']);
        $this->set('posts', $posts);

        //Get the base URL of your website
        $url = Router::url('/', true);
        $this->set('url', $url);

    }

}   

Step 3. Create a view file for index action containing XML tags: index.ctp

<url>
    <loc><?= $url; ?></loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
</url>
<url>
    <loc><?= $url; ?>contact-us</loc>
    <priority>0.5</priority>
</url>
<url>
    <loc><?= $url; ?>above-us</loc>
    <priority>0.5</priority>
</url>
<url>
    <loc><?= $url; ?>service</loc>
    <priority>0.5</priority>
</url>
<?php foreach($osts as $post){?>
<url>
    <loc><?php echo $url.'blog/'.$post['slug'] ?></loc>
</url>
<?php } ?>

Step 4. Add a route for sitemap in routes.php:

Router::scope('/', function (RouteBuilder $routes) {
    // Other routes.
    $routes->connect('/sitemap.xml',['controller'=>'Sitemaps','action'=>'index']);
});

For the detailed tutorial, you can visit this tutorial and learn step by step how to create an XML sitemap in CakePHP.

Overblown answered 2/1, 2021 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.