How to create a sitemap in cakephp?

Member

by bertha , in category: SEO , 2 years ago

How to create a sitemap in cakephp?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

2 answers

Member

by emelie , 2 years ago

@bertha 

In CakePHP, you can create a sitemap by following these steps:

  1. Install the "CakePHP Sitemap" plugin using composer:
1
composer require dereuromark/cakephp-sitemap


  1. Load the plugin in your CakePHP application by adding the following line in config/bootstrap.php:
1
Plugin::load('CakeSitemap');


  1. Create a SitemapController with the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace AppController;

use CakeSitemapSitemapSitemap;
use CakeSitemapSitemapSitemapIndex;
use CakeSitemapDatasourceSitemapSource;

class SitemapController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    }

    public function index()
    {
        $sitemap = new Sitemap();
        $sitemap->addUrl(['url' => '/', 'changefreq' => 'daily', 'priority' => '1.0']);

        $this->set(compact('sitemap'));
        $this->set('_serialize', ['sitemap']);
    }

    public function sitemap()
    {
        $sitemap = new Sitemap();
        $sitemapSource = new SitemapSource();
        $urls = $sitemapSource->getUrls();

        foreach ($urls as $url) {
            $sitemap->addUrl($url);
        }

        $this->response->type('xml');
        $this->set(compact('sitemap'));
        $this->set('_serialize', ['sitemap']);
    }

    public function sitemapIndex()
    {
        $sitemapIndex = new SitemapIndex();

        $sitemapIndex->addSitemap(['url' => ['controller' => 'Sitemap', 'action' => 'sitemap']]);

        $this->response->type('xml');
        $this->set(compact('sitemapIndex'));
        $this->set('_serialize', ['sitemapIndex']);
    }
}


  1. Create a sitemap.ctp view file in src/Template/Sitemap/ with the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?= $this->Xml->header() ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php foreach ($sitemap->getUrls() as $url): ?>
        <url>
            <loc><?= h($url['url']) ?></loc>
            <lastmod><?= $this->Time->format($url['lastmod'], 'W3C') ?></lastmod>
            <?php if (isset($url['changefreq'])): ?>
                <changefreq><?= h($url['changefreq']) ?></changefreq>
            <?php endif ?>
            <?php if (isset($url['priority'])): ?>
                <priority><?= h($url['priority']) ?></priority>
            <?php endif ?>
        </url>
    <?php endforeach ?>
</urlset>


  1. Create a sitemap_index.ctp view file in src/Template/Sitemap/ with the following code:
1
2
3
4
5
6
7
8
<?= $this->Xml->header() ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php foreach ($sitemapIndex->getSitemaps() as $sitemap): ?>
        <sitemap>
            <loc><?= $this->Url->build($sitemap['url'], true) ?></loc>
        </sitemap>
    <?php endforeach ?>
</sitemapindex>


  1. Create a getUrls() method in src/Model/Datasource/SitemapSource.php to get the URLs you want to include in your sitemap.
1
2
3
4
<?php
namespace AppModelDatasource;

use Cake


by percy_bauch , 2 years ago

@bertha 

To create a sitemap in CakePHP, you can follow these steps:

  1. First, create a new action in your controller that will generate the sitemap. For example, let's call it sitemap.
1
2
3
public function sitemap() {
    // your code to generate the sitemap
}


  1. In the routes.php file of your CakePHP application, create a new route for the sitemap action:
1
Router::connect('/sitemap.xml', array('controller' => 'your_controller', 'action' => 'sitemap'));


This will create a new route that maps the URL /sitemap.xml to your sitemap action.

  1. Now you can start building your sitemap in the sitemap action. You can use CakePHP's built-in Xml class to generate the sitemap in XML format:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public function sitemap() {
    $this->autoRender = false;

    App::uses('Xml', 'Utility');
    $xml = Xml::build('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');

    // add URLs to the sitemap
    $url = $xml->addChild('url');
    $url->addChild('loc', Router::url(array('controller' => 'pages', 'action' => 'home'), true));
    $url->addChild('changefreq', 'daily');
    $url->addChild('priority', '1.0');

    // add more URLs ...

    // output the sitemap
    $this->response->type('xml');
    $this->response->body($xml->asXML());
}


In this example, we are adding one URL to the sitemap with a loc element that points to the home page of our CakePHP application. You can add more URLs to the sitemap by creating additional url elements and setting the appropriate values for the loc, changefreq, and priority elements.

  1. Finally, you can test your sitemap by accessing the URL /sitemap.xml in your browser. You should see the sitemap XML displayed in the browser.


That's it! You have now created a sitemap in CakePHP.