</> Code The Pixel

CakePHP 4 XML SiteMap

Asyraf Wahi Anuar - December 13, 2021

Estimated reading time: 3 minutes, 21 seconds

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:

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.
File location: …\src\Controller\SitemapsController.php

<?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

<?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

<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 :)


Cite this article (APA 6th Edition)

Popular
CakePHP 4 Print PDF Using CakePDF
May, 17 2020
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 jQuery Date Time Picker
October, 01 2018
CakePHP 4 Export To CSV
May, 29 2020
CakePHP 4 Authentication Using...
May, 11 2020
CakePHP 4 File Upload Using Proffer Plugin
May, 15 2020
CakePHP 4 Find, Sort & Count
June, 02 2020
Share
Sharing Link
Click the icon to share