Search code examples
codeigniter-3

Sitemap for dynamic website - codeigniter


I have a dynamic website designed with Codeigniter 3 and I am working on the sitemap part as a newbie.

I found the library sitemap-php from evert/sitemap-php but I can't make it run.

From now this is what I did, I put the Sitemap.php file into my library folder

Controller:

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

class Deals extends CI_Controller {

public function __construct(){

    parent::__construct();
    $this->load->helper('url', 'form', 'security');
    $this->load->library('form_validation');
    $this->load->library('session');
    $this->load->library('email'); 
    $this->load->model('deal_model');
    $this->load->helper(array('cookie','custom','text'));
}

    public function Sitemap(){ 

      $this->load->library('Sitemap'); 
      $sitemap = new Sitemap('https://www.mywebsite.com');
      $sitemap->setPath('/public_html/Sitemap/'); // I created a folder Sitemap into my public folder
      $sitemap->setFilename('sitemap');
      $sitemap->addItem('/', '1.0', 'daily', 'Today');
      $sitemap->createSitemapIndex('https://www.mywebsite.com/sitemap/', 'Today');
}

Then when I go to https://www.mywebsite.com/sitemap/, I have an Error 404.

Could you guide me to solve my issue.

Thanks


Solution

  • The docs for that library describe that it generates a static XML file. The code you've shown will do that - but your code is in a Library, and you have not run it yet. You need to run it, then it will generate an XML file as you've specified, in /public_html/Sitemap/. From your description you are looking for the XML before doing anything to generate it, and it does not (yet) exist.

    From your updated code, you now have the code to generate the static XML available as a Controller method. According to the standard Codeigniter routing conventions, the method you have created is accessible at:

    http://your-site/deals/Sitemap
    

    (Maybe you've also set up some routes so it is accessible at other URIs also.)

    Visit that URL, once, to generate the static XML file at /public_html/Sitemap/sitemap.xml. Assuming your code works, you should then be able to browse the XML at

    http://your-site/Sitemap/sitemap.xml
    

    Side note: AFAIK Codeigniter convention is for capitalised Controller file and class names (Deal.php and Deal), but all lower-case method names (sitemap() instead of Sitemap()). You can see examples of this in the Controller docs I linked above. I am not sure if it matters, just pointing it out.