Create Dynamic XML Sitemap in Codeigniter App – Easily

October 15, 2020

Your dynamic website might need to have an XML Sitemap if you really care about the SEO (Search Engine Optimization). Here is how to create a dynamic XML Sitemap in the Codeigniter project easily.

Dynamic XML Sitemap in Codeigniter

Step 1: First, we need to create a Controller named Sitemap in the applications/controllers folder.

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 

class Sitemap extends CI_Controller { 

	public function __construct() { 
		$this->load->model('Item_model'); //Loading Items Model Class

	public function index()
		$data['items'] = $this->Item_model->get_all(); //Getting items list from database
		$this->load->view('sitemap', $data);

Step 2: Then, create a view file in the applications/views folder and name it sitemap.php.

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<urlset xmlns=\"\">";

echo "<url>".
if (isset($items)){
	foreach($items as $item) {
		echo "<url>".
echo "</urlset>";

Step 3: Add route code to the routes.php in the applications/config folder.

$route['sitemap.xml'] = "Sitemap/index";

Now check your XML sitemap

Tagged in:
About Author
Adeeb C (227 Articles)

Adeeb C is a Web Designer, Web Application Developer, Android App Developer, WordPress Developer, Professional Blogger. He has 5 years of experience in WordPress, Web Developing, Professional Blogging, and 2 years of experience in Android App Developing.