@bertha
In CakePHP, you can create a sitemap by following these steps:
1
|
composer require dereuromark/cakephp-sitemap |
1
|
Plugin::load('CakeSitemap'); |
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 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 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 2 3 4 |
<?php namespace AppModelDatasource; use Cake |
@bertha
To create a sitemap in CakePHP, you can follow these steps:
1 2 3 |
public function sitemap() { // your code to generate the sitemap } |
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 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.
That's it! You have now created a sitemap in CakePHP.