@dorothea
To create a sitemap for a CakePHP website, you can follow these steps:
1
|
$routes->connect('/sitemap', ['controller' => 'Pages', 'action' => 'sitemap']); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function sitemap() { $this->RequestHandler->respondAs('xml'); $this->loadModel('YourModel'); $pages = $this->YourModel->find('all', [ 'fields' => [ 'YourModel.id', 'YourModel.created', ], 'order' => [ 'YourModel.created' => 'desc', ], ]); $this->set(compact('pages')); } |
Replace 'YourModel'
with the appropriate model name that represents the pages you want to include in the sitemap.
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="UTF-8" ?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <?php foreach ($pages as $page): ?> <url> <loc><?= h($this->Url->build('/', true) . 'pages/view/' . $page['YourModel']['id']) ?></loc> <lastmod><?= h(date('Y-m-dTH:i:s+00:00', strtotime($page['YourModel']['created']))); ?></lastmod> </url> <?php endforeach; ?> </urlset> |
Make sure to update 'pages/view/' . $page['YourModel']['id']
with the appropriate URL structure for your website's pages.
Remember to regenerate the sitemap whenever you add or update pages on your website to ensure it stays up-to-date.