Create Dynamic XML Sitemap in CodeIgniter
Learn how to generate a dynamic sitemap.xml using CodeIgniter 3 step by step.
CodeIgniter 3 Dynamic Sitemap SEO TutorialWhat is Sitemap?
A XML Sitemap is a file that contains the URLs of your website. Search engines like Google, Bing and Yahoo read this file to discover and crawl your pages more efficiently.
A sitemap can also include additional information such as:
- Page URL
- Last Modified Date
- Priority
- Change Frequency
sitemap.xml using CodeIgniter and MySQL.Step 1 — Create Database
Create a database named codeigniter_sitemap.
CREATE DATABASE codeigniter_sitemap;
Step 2 — Create Pages Table
Create a table called pages.
CREATE TABLE pages (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name TEXT NULL,
url TEXT NULL,
description LONGTEXT NULL
);
| Column | Type |
|---|---|
| id | INT |
| name | TEXT |
| url | TEXT |
| description | LONGTEXT |
Step 3 — Configure CodeIgniter
Open the following files:
application/config/config.phpapplication/config/autoload.php
$config['base_url'] = 'http://localhost/codeigniter-dynamic-sitemap/';
$autoload['helper'] = array('url');
$autoload['model'] = array('crud');
$autoload['libraries'] = array('database');
Step 4 — Database Connection
Edit application/config/database.php
$db['default'] = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter_sitemap',
'dbdriver' => 'mysqli',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci'
);
Step 5 — Create Route
Open application/config/routes.php
$route['sitemap\.xml'] = "welcome/sitemap";
sitemap() function.Step 6 — Create Crud Model
Now create a model file named Crud.php inside the application/models/ folder.
The model will retrieve all records from the pages table using CodeIgniter's database library.
File location:
application/models/Crud.php
Complete Code:
<?php
class Crud extends CI_Model
{
public function get_data($table)
{
$data = $this->db->get($table);
return $data->result();
}
}
get_data() function receives the table name, fetches all records from that table and returns the results as an array of objects.In our example, the controller will call:
$this->crud->get_data("pages");
This will retrieve all records from the pages table.
Step 7 — Create Sitemap Controller
Edit application/controllers/Welcome.php
public function sitemap()
{
$data['ALLPAGES'] = $this->crud->get_data("pages");
header("Content-Type: text/xml;charset=utf-8");
$this->load->view("sitemap", $data);
}
Step 8 — Create sitemap.php View
Now create a view file named sitemap.php inside the application/views/ folder.
File location:
application/views/sitemap.php
This view will generate the XML Sitemap using the page URLs retrieved from the database.
Complete Code:
<?xml version="1.0" encoding="UTF-8"?>
<?php foreach ($ALLPAGES as $page) { ?>
<?= base_url($page->url); ?>
0.5
<?php } ?>
foreach loop goes through every record in the pages table and creates a separate entry for each page.Example XML Output
Suppose your database contains these URLs:
/about-us
/courses
/contact-us
The generated sitemap will look similar to:
<?xml version="1.0" encoding="UTF-8"?>
https://www.example.com/about-us
0.5
https://www.example.com/courses
0.5
https://www.example.com/contact-us
0.5
sitemap.php file. The first code block is the actual PHP view code. The second example only shows what the browser/search engine will receive after PHP generates the sitemap.Final Output
Open the following URL in your browser:
http://localhost/codeigniter-dynamic-sitemap/sitemap.xml
Project Folder Structure
application
│
├── config
│ ├── config.php
│ ├── routes.php
│ └── database.php
│
├── controllers
│ └── Welcome.php
│
├── models
│ └── Crud.php
│
└── views
└── sitemap.php
Conclusion
Congratulations! You have successfully created a dynamic XML Sitemap in CodeIgniter. Whenever a new page is added to the pages table, it will automatically appear in sitemap.xml without manually editing the file.
This is the recommended approach for SEO because search engines can always discover your latest pages through an updated sitemap.