How To Become A Programmer Without A Degree & Experience

CakePHP3 – How to Create a Sitemap Without a Plugin


 

 

Introduction

In this post, I’m going to share how to create a sitemap without a plugin.

I found cakephp3-sitemap plugin, but the installation by composer doesn’t work. (You can download manually, though.)

I’m not a big fan of using plugins, and also my project doesn’t have many Controllers and Views. So, I did it by myself.

The steps are exactly the same as when you create pages with CakePHP.
Create Layout, Controller, and View. That’s it!

I also confirmed that being able to submit a sitemap to Google Search Console.

Hope this helps 🙂
 
 

Environment

CakePHP 3.6.10
PHP 7.1.19

 

Sample Data

I assume that the website has these pages.

Top Page https://example.com/
Information Page https://example.com/info
Contact Page https://example.com/contact
Display data from Items table.
(1, 2, 3 are item_id.)
https://example.com/items/detail/1
https://example.com/items/detail/2
https://example.com/items/detail/3

 
 
 

Controller

Create /src/Controller/SitemapsController.php

 
 
 

Layout

Create /src/Template/Layout/sitemap.ctp

 
 
 

View

Create /src/Template/Sitemaps/index.ctp

 

About Tags

<loc> Required The page url.
<changefreq> Optional Update Frequency. Example: always, daily, yearly
<priority> Optional Valid values are from 0.0 to 1.0.

For more details, please check here.
 
 
 

Route

Now, the url of this sitemap is “https://example.com/sitemaps/index”
Let’s change this to “https://example.com/sitemap.xml”

Open /config/routes.php, then add this code around line 57.

 
 

Let’s access https://example.com/sitemap.xml.
Were you able to generate the sitemap?
 
 
 

Submit

Finally, let’s submit the sitemap to Google Search Console.

Step1: Log in to Search Console and select your site.

Step2: Click [Sitemaps]

Step3 : Click the red [ADD/TEST SITEMAP] button.

Step4 : Type [sitemap.xml], then click [Test] button.
 
 

 

If there’s no problem with the test, type [sitemap.xml] again, then click [Submit] button.
 
 
 
It’s all done!