How to Create Dynamic XML Sitemap

How to Create Dynamic XML Sitemap in Codeigniter

How to create a dynamic XML sitemap in the CodeIgniter application is really important to crawl website pages links or URLs for Google, Yahoo. Every basic website needs an XML sitemap so that we can submit the XML sitemap to the google search console tool to index our website in google or yahoo under the top 10 lists on the first page. So here I will explain every step with you how to create a dynamic XML sitemap in the CodeIgniter application.

Here, you only have to follow every step in CodeIgniter to add an XML sitemap for SEOs. I just created one table and store all the product information, slug URLs to create a dynamic Codeigniter google sitemap generator.

Navigate of the site is described as a sitemap. Users can search easily by sitemap to navigate the site. It’s being created in XML coding.

So you just follow the route, controller, and view the code file below.

 

Step-1: Create a Sitemap controller file in your project folder.

In this step, We create a Sitemap.php file in this path application/controllers/Sitemap.phpand put bellow code in this file:

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class Sitemap extends CI_Controller {

public function index()    {

$this->load->database();

$query = $this->db->get(“products”);

$data[‘products’] = $query->result();

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

} }

Step-2: Create Route

In this step, we create one “sitemap.xml” route for access from the URL. so open the application/config/routes.php file and add code as below.

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

$route[‘default_controller’] = ‘welcome’;

$route[‘404_override’] = ”;

$route[‘translate_uri_dashes’] = FALSE;

$route[‘sitemap.xml’] = “Sitemap/index”;

 

Step-3: Create a sitemap view file in your project folder.

In this step, We will create a sitemap.php view file in this pathapplication/views/sitemap.php. Write the design of the XML file with a dynamic code. So let’s copy and paste the below code:

echo'<?xml version=”1.0″ encoding=”UTF-8″ ?>’

<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> phpopen echo base_url();phpoff</loc>

<priority>1.0</priority>

<changefreq>daily</changefreq>

</url>

<!– Sitemap –>

phpon foreach($products as $product) { phpof

<url>

<loc><?php echo base_url().”product/”.$product->id ?></loc>

<priority>0.5</priority>

<changefreq>daily</changefreq>

</url>

}

</urlset>

 

So let’s run and see how it looks your XML file.

Create Dynamic XML Sitemap in Codeigniter – Vkprogramming

 

Conclusion

I hope you found this tutorial helpful for your project. Creating Dynamic XML Sitemap in PHP Codeigniter is an important feature for any web application. So here we are, completed the basic step to generate a dynamic XML sitemap.

 

How To Create Dynamic Xml Sitemap In Codeigniter

A sitemap is described navigate of the website. users can easily search to navigate the site by sitemap. it is creating in the XML coding.

We have given below example of how to create dynamic XML sitemap using Codeigniter. Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file.

So you can follow below steps.

 

  1. Create a Sitemap controller file in your project folder.

Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file.

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);`

class Sitemap extends CI_Controller {

    function __construct() {

        parent::__construct();    }

    public function index()    {

 $que = ‘SELECT * FROM product’;

        $arrData[‘product_detail’] = $this->db->query($que)->result_array();

 $this->load->view(‘sitemap’, $arrData);    }}

 

  1. Create sitemap view file in your project folder.

Add bellow code in this file. in this bellow code in product name wise create dynamic xml sitemap and when this code run at that time sitemap.xml file automatically uploaded on your root directory of project folder.

$xmlString = ‘<?xml version=”1.0″ encoding=”UTF-8″

    <urlset

 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

 xmlns:image=”http://www.google.com/schemas/sitemap-image/1.1″

 xsi:schemaLocation=”http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd”

 xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9″>

<url>

    <loc>http://www.example.com</loc>

</url>’;

foreach ($product_detail as $pd) {

    $xmlString .=   ‘<url>’;

 $xmlString .=  ‘<loc>’.base_url(‘product/’.$pd[‘name’].'</loc>’;

    $xmlString .=  ‘</url>’;}

$xmlString .= ‘</urlset>’;

$dom = new DOMDocument;

$dom->preserveWhiteSpace = FALSE;

$dom->loadXML($xmlString);

if($dom->save($_SERVER[“DOCUMENT_ROOT”].’/sitemap.xml’)){

    echo “<h2>Site Map Created SuccessFully</h2>”;

}else{

    echo “<h2>Site Map Created Failed</h2>”;

}

Related searches For How to Create Dynamic XML Sitemap

Leave a Comment

Your email address will not be published. Required fields are marked *