New

Get 25% off 🎉 on DIGITAL MARKETING Course | Hurry Up!

New

Get 25% off 🎉 on DIGITAL MARKETING Course | Hurry Up!

How to Create a Dynamic Sitemap in CodeIgniter
Digital Marketing

How to Create a Dynamic Sitemap in CodeIgniter

How to Create a Dynamic Sitemap in CodeIgniter

A sitemap is a document which contains our website's URL. Search Engines (Google, Yahoo, Bing) read this file in order to crawl your website. In a sitemap file we can include additional information of our pages , such as the date of addition, as well as modified dates to let crawlers of search engines better understand when one of the pages were added as well as when they were modified.

In this blog we will learn how to create a dynamic sitemap in CodeIgniter, let's see the following steps:

Step1: Create a Database name codeigniter_sitemap

CREATE DATABASE codeigniter_sitemap;

Step2:  Create a table page in the database:

CREATE TABLE pages (

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

name TEXT  NULL,

url TEXT NULL,

   description LONGTEXT NULL

);

Step3:  Setup basic configuration in your project like base URL in config.php file, autoload necessary libraries, helper in autoload.php file.

$config['base_url'] = ' http://localhost/codeigniter-dynamic-sitemap;

$autoload['helper'] = array('url');

$autoload['model'] = array('crud');

$autoload['libraries'] = array('database');

Step4: Setup Database Connection in application>config>database.php file.

$db['default'] = array(

'dsn'   => '',

'hostname' => 'localhost',

'username' => 'root',

'password' => '',

'database' => 'codeigniter_sitemap',

'dbdriver' => 'mysqli',

'dbprefix' => '',

'pconnect' => FALSE,

    'db_debug' => (ENVIRONMENT !== 'production'),

'cache_on' => FALSE,

'cachedir' => '',

'char_set' => 'utf8',

'dbcollat' => 'utf8_general_ci',

'swap_pre' => '',

'encrypt' => FALSE,

'compress' => FALSE,

'stricton' => FALSE,

'failover' => array(),

'save_queries' => TRUE

);

Step5: Create Route

Go to the following  application>config>routes.php open routes.php and write down the following code:

$route['sitemap\.xml'] = "welcome/sitemap";

Step6: Create a model file Crud.php in the models folder:

<?php

class Crud extends CI_Model{

         function get_data($table)

         {  

           $data= $this->db->get($table);

           return $data->result();               

         }

}

Step7:  Create a function sitemap() in you default controller:

application>controllers>Welcome.php open the following file and write down the following code:

public  function sitemap() 

{

     $data['ALLPAGES']=$this->crud->get_data("pages");

     header("Content-Type: text/xml;charset=iso-8859-1");

     $this->load->view("sitemap",$data);

}

Step8: Create a view sitemap.php

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

<url>

     <loc><?= base_url();?></loc>

     <priority>1.0</priority>

</url>

<?php foreach($ALLPAGES as $page) { ?>

<url>

     <loc><?= base_url($page->url) ?></loc>

     <priority>0.5</priority>

</url>

<?php } ?>

</urlset>

See the output open the following url :

http://localhost/codeigniter-dynamic-sitemap/sitemap.xml

  • Share

Previous Post

What is graphic design

Next Post

Robots.txt file Structure

google review