How to create XML sitemap in Cakephp – Very Simple Method

Spread the love

 

xml sitemap in cakephp



In this tutorial, I will share with you a step-by-step process of creating an XML sitemap in Cakephp 3 in detail. 

 

In the custom Cakephp application, we always have to make the sitemaps as per the URLs created either by the controller routes and/or by the manual routing set in the routes.php file. 

 

Why create an XML sitemap in Cakephp

You may need the XML sitemap to submit to the Google search console for SEO purposes, which is a very important step to rank your website or application on the search engine. 

 

Now if you want to generate the XML sitemap in Cakephp by using a simple XML sitemap URL like www.mywebsite.com/sitemap.xml then you need to follow these simple steps. 

Steps to create an XML sitemap in CakePHP 3:

1. Decide which page URLs you want to include in the sitemap. 

2. Create a separate layout file for the sitemap view. 

3. Create a separate controller for the sitemap generator. 

4. Create a template view file for the sitemap display. 

5. Add a separate route for the sitemap URL in the routes.php file.

 

Now let’s see the steps in detail to create XML sitemap in Cakephp:

 

Step 1: Decide which page URLs you want to include in the XML sitemap in Cakephp

 

Let’s suppose your site contains some important pages which you want to rank in SEO and want to show in search result pages.

 

For example in my dummy site www.mywebsite.com I want the below pages to show up in the sitemap out of which some are static pages and some are dynamic pages that will be generated by some controller actions.

 

Static pages: 

  1. www.mywebsite.com 
  2. www.mywebsite.com/contact-us 
  3. www.mywebsite.com/about-us 
  4. www.mywebsite.com/services

 

Dynamic pages: These pages are the lists of blog posts you created on the site and the list of these posts will be generated by the PostsController using index action.

  1. www.mywebsite.com/blog/post1 
  2. www.mywebsite.com/blog/post2
  3. www.mywebsite.com/blog/post3
  4. www.mywebsite.com/blog/post4

 

Step 2: Create a separate layout file for the sitemap view

In this step, you will need to create a sitemap layout file: “sitemap.ctp” at the location: “src/Template/Layout/sitemap.ctp’

This template file should have an XML header and <urlset> tag.

sitemap.ctp:

 

<?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 3: Create a separate controller for the sitemap generator 

 

Let’s create a SitemapsController in your project folder at a path “src/Controller/SitemapsController.php” and add one action – index in this controller.

 

In the index action, you need to do the following steps: 

a. Set the layout for the sitemap

b. Add a RequestHandler component for the XML view.

 

The code will look as below: SitemapsController.php:

 

<?php
namespace AppController;

 

use AppControllerAppController;
use CakeORMTableRegistry;
use CakeRoutingRouter;

 

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 4: Create a template view file for the sitemap display 

 

Now create a view file for index action as per the SitemapsController named “index.ctp” at the location: “src/Template/Sitemaps/index.ctp”. 

 

In the index.ctp file you will be going to display the list of URLs of all the pages you want in the sitemap. 

 

The URLs will need to be displayed inside the <url></url> tags. 

A sample <url> tag looks like this:

 

<url>
    <loc>http://www.mywebsite.com</loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
</url>

 

A short description of the above tags used to create an XML sitemap in Cakephp: 

 

a. <url>: Contains each page URL with its location, change frequency and the priority of page to be indexed. 

b. <loc>: This is a required tag. It contains the URL of the page. 

c. <changefreq>: This tag is optional. It shows the frequency of the content update of the page. The values of changefreq would be – always, daily, weekly, yearly. 

d. <priority>: This tag is optional. This tag indicates the priority of a page which needs to be indexed. The possible values of these tags are between 0.0 to 1.0 Now, add the below code to the “index.ctp” file: 

 

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

 

Now if you access the URL of controller SitemapsController’s index action i.e. “www.mywebsite.com/Sitemaps/index” you will get an XML file format display in your browser. 

 

The first four <url> tags will contain the static pages and the foreach loop will generate the dynamic blog posts page URLs in the sitemap, as we discussed in step 1. 

 

If you want to learn more details about the tags present in the above file you can visit this website.

 

Step 5: Add a separate route for the sitemap URL in the routes.php file 

 

In the above step, we have generated the sitemap URL as per the controller action. 

Now if you want to submit the URL with the “XML” extension, like “www.mywebsite.com/sitemap.xml” you need to add a separate route in the routes.php file. 

 

Add below the line of code in your routes.php file present at the location /config/routes.php.

 

routes.php:

 

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

 

Now, you can access your XML sitemap in Cakephp using the URL: www.mywebsite.com/sitemap.xml in the browser. 

You can now easily submit this XML sitemap in Cakephp URL to any search engine for SEO and search engine visibility purposes.

Hope you have liked this tutorial for creating an XML sitemap in Cakephp. Please comment if you are having any doubts or questions. I will try to solve them as early as possible.

 

Thank you!


Spread the love

Leave a Comment