How to quickly create sitemap in CakePHP 3?

So you want to create a sitemap for you Cakephp 3 website in less than 2 steps? Follow the guide!

What’s a sitemap and why i need it?

First things first, a sitemap is a xml document wich contains a list of urls belongs to your website. It’s help robots like Google, Bing and so many others to have a better understanding of yout website and so to crawl it at optimal way. It’s like a plan to simplify.

Step 1 – Create a route

Open up your file named routes.php in config folder and create the route like this.

Router::scope('/', function (RouteBuilder $routes) {
    
    [...]

    /**
     * ...and connect the rest of 'Pages' controller's URLs.
     */
    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);

    $routes->connect('/sitemap', ['controller' => 'pages', 'action' =>'sitemap'],['_ext'=>'xml']);

    [...]

    $routes->fallbacks(DashedRoute::class);
});

You can change the controller and the action by what you want.

Step 2 – Create the action in your controller

So if you take my example go to the file called PagesController and create an action called sitemap like this.

    public function sitemap()
    {
        $baseUrl = 'https://' . $this->request->getEnv('HTTP_HOST');
        $this->autoRender = false;

        $sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.sitemaps.org/schemas/sitemap-image/1.1" >' . "\n";
        $sitemap .= '<url>
                        <loc>'.$baseUrl.'</loc>
                        <priority>1</priority>
                     </url>' . "\n";
        //news
        $sitemap.= '<url>
                        <loc>' . $baseUrl .'/news.html</loc>
                        <priority>0.5</priority>
                    </url>';

        //contact
        $sitemap.= '<url>
                        <loc>' . $baseUrl .'/contact.html</loc>
                        <priority>0.5</priority>
                    </url>
                  ';

       /* INSERT YOUR LOOPS LOGIC HERE */

        $sitemap .= "\n" . '</urlset>';
        $sitemap = Xml::build($sitemap);
        $sitemap =  $sitemap->asXML();
        
        return $this->response->withType('text/xml')->withStringBody($sitemap);
    }

This is a quickly example. You can put whatever you want with your logic.
If you need to take all the Categories then just do your request and an extra loop to fill your sitemap.
And that’s it if you go to https://mywebsite.com/sitemap.xml you will see it.

Bonus: Don’t forget to indicate the sitemap in your robots.txt

Why ? Because if you name your route like foobar.xml instead of sitemap.xml. The robots don’t have a change to find it naturally. For that you can put the url in the robots.txt file.

If you don’t have one. Juste create a robots.txt file in weebroot folder and put this inside.

User-agent: *
Disallow:
Sitemap: https://mywebsite.com/sitemap.xml

If you need more infos about robots txt and sitemap, here two links for that.
https://support.google.com/webmasters/answer/6062608?hl=en
https://support.google.com/webmasters/answer/183668?hl=en

Leave a Reply