Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 XML SiteMap

Asyraf Wahi Anuar 2 min read

A sitemap is a file that contains information about the site's pages, contents, and other files, as well as their relationships. This file is read by search engines like Google to crawl the site contents more efficiently. This tutorial provides an overview of how to create an XML sitemap in CakePHP...

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

A sitemap is a file that contains information about the site's pages, contents, and other files, as well as their relationships. This file is read by search engines like Google to crawl the site contents more efficiently. This tutorial provides an overview of how to create an XML sitemap in CakePHP 4. Click here to visit Code The Pixel XML sitemap.

Configure Routes

The routes are used to redirect sitemap.xml to the specific sitemap controller. Navigate to …\config\routes.php and add the following code:

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

SiteMap Controller

Create a new controller for the sitemap. In the public function index, add your specific table (eg: articles, blogs, projects). The following code example also includes the unauthenticated method in public function beforeFilter to make the sitemap index public.

src/Controller/SitemapsController.phpPHP
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;

class SitemapsController extends AppController
{
    public function beforeFilter(\Cake\Event\EventInterface $event)
    {
        parent::beforeFilter($event);
        $this->Authentication->addUnauthenticatedActions(['index']);
    }
    
    public function index()
    {
        $this->viewBuilder()->setLayout('sitemap');
        $this->RequestHandler->respondAs('xml');
        //Articles Table
        $articleTbl = TableRegistry::getTableLocator()->get('Articles');
        $articles = $articleTbl->find()->select(['slug','modified']);
        $this->set('articles', $articles);
        //Blogs Table
        $blogTbl = TableRegistry::getTableLocator()->get('Blogs');
        $blogs = $blogTbl->find()->select(['slug','modified']);
        $this->set('blogs', $blogs);
        //Projects Table
        $projectTbl = TableRegistry::getTableLocator()->get('Projects');
        $projects = $projectTbl->find()->select(['slug','modified']);
        $this->set('projects', $projects);
        
        //debug($articles);
        //exit;

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

    }

}
?>

SiteMap View

Create a new sitemap layout at …\templates\layout\sitemap.php

templates/layout/sitemap.phpPHP
<?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>';
?>

Then create the sitemap index at …\templates\Sitemaps\index.php

templates/Sitemaps/index.phpPHP
<url>
    <loc>https://yourDomainName.com</loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
</url>
<url>
    <loc><?= $url; ?></loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
</url>
<url>
    <loc><?= $url; ?>articles</loc>
    <priority>0.8</priority>
</url>
<url>
    <loc><?= $url; ?>blogs</loc>
    <priority>0.8</priority>
</url>
<url>
    <loc><?= $url; ?>projects</loc>
    <priority>0.8</priority>
</url>
<url>
    <loc><?= $url; ?>contact</loc>
    <priority>0.5</priority>
</url>
<?php foreach($articles as $article){?>
<url>
    <loc><?php echo $url.'articles/'.$article->slug ?></loc>
    <lastmod><?php echo $article->modified ?></lastmod>
    <priority>0.8</priority>
</url>
<?php } ?>
<?php foreach($blogs as $blog){?>
<url>
    <loc><?php echo $url.'blogs/'.$blog->slug ?></loc>
    <lastmod><?php echo $blog->modified ?></lastmod>
    <priority>0.8</priority>
</url>
<?php } ?>
<?php foreach($projects as $project){?>
<url>
    <loc><?php echo $url.'projects/'.$project->slug ?></loc>
    <lastmod><?php echo $project->modified ?></lastmod>
    <priority>0.8</priority>
</url>
<?php } ?>

At this point, the XML sitemap is ready to view at yourDomain.com/sitemap.xml

That’s all. Happy coding :)

Did this tutorial help?

259 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

Using Bahasa Melayu Table Names in CakePHP

Learn how to use Bahasa Melayu table names like "pengguna" in CakePHP while following the framework’s naming conventions. Includes tips for using the Bake tool and avoiding pluralization issues.

5 min read

Tools & Software

Formatting PHP in VSCode Using Prettier and PHP Intelephense

Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.

2 min read