Generate a sitemap or sitemap index file for your web application with the CodeIgniter 4 PHP framework. This CodeIgniter 4 model quickly enables you to generate a valid SEO-friendly sitemap file for you web application and/or website. An XML sitemap file helps crawlers of search engines to easily navigate and browse through your website and understand the structure of the different urls.
Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.
Web crawlers usually discover pages from links within the site and from other sites. Sitemaps supplement this data to allow crawlers that support Sitemaps to pick up all URLs in the Sitemap and learn about those URLs using the associated metadata. Using the Sitemap protocol does not guarantee that web pages are included in search engines, but provides hints for web crawlers to do a better job of crawling your site.
Read more about the Sitemap protocol on sitemaps.org.
Copy the files in the app folder into your CodeIgniter 4 app folder. This includes the Models/SitemapModel.php and Controllers/SitemapController.php files and examples Controllers/BlogController.php, Entities/BlogEntity.php and Models/BlogModel.php files. Follow the instructions below on how to use the model in your own controller.
The model is designed to be called and controlled by a CodeIgniter 4 controller. The model create and save an actual XML-file to be saved on the disk. It generates a valid XML-file upon each action such as create, edit and delete from the controller.
- Add the
SitemapModel.php
to your Models folder andSitemapController.php
to your Controllers folder. - Edit the
SitemapController.php
according to your own system. There is 2 examples inSitemapController.php
; -
- Static Example
general()
: You can add static link such as in the function.
- Static Example
-
- Dynamic Example
blog()
: You need a Model or DB query for the dynamic system. I have added an example for Model systems.
- Dynamic Example
Check the BlogController.php
file to generate sitemap files automatically.
There are 3 action type in BlogController.php
;
- Add Blog
- Edit Blog
- Delete Blog
If there is no error during action (insert, update, delete), Add the code given below after insert, save, delete actions.
// Generate Sitemap
$this->sitemapController;
Thanks to Gerard Nijboer to create in the CodeIgniter 3 code structure.