CodeIgniter Sitemap Generator Model
Generate a sitemap or sitemap index file for your web application with the CodeIgniter PHP framework. This CodeIgniter 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. What are sitemaps?
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.
Copy the files in the application folder into your CodeIgniter application folder. This includes the model file and an example controller file. Follow the instructions below on how to use the model in your own controller. Installation
The model is designed to be called and controlled by a CodeIgniter controller. The model does not create and save an actual XML-file to be saved on the disk. Rather, it generates a valid XML-file upon each request and directly serves it to the controller and thus the client browser.
This means that two examples of sitemap files to submit to search engines could look like the following urls:
You can load the sitemap model from your CodeIgniter controller with the following line of code: Load the model
To add an item to the sitemap, use the following line of code: Add an item
$this->sitemapmodel->add('http://www.example.com/contact', '2015-11-02', 'monthly', 0.9);
This function checks for the validity of the item on two points:
- The value of the parameter changefreq must be one of the following options:
- The value of the parameter priority must be a value between 0 and 1
If any of the checks fail, an error is triggered by the model.
When you have inserted all the items in the sitemap, you can call the SEO-friendly valid XML-code by calling a simple line of code: Generate a sitemap file
A sitemap index file is a sitemap pointing to one or more other sitemap files. This helps search engine crawlers to easily understand the diversity of sitemaps that are served by your website. To call a sitemap index file instead of a regular sitemap file, simply add the string 'sitemapindex' as a parameter to the output-function, like this: Generate a sitemap index file
If you wish to clear the items in the sitemap, call the following function: Clear the sitemap