CodeIgniter Forums

Full Version: Best Way To Add Sitemap
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am not the programmer that developed my software but I do have basic knowledge with CI. I want to add a sitemap to my website and I'm not sure how to do this. I Googled a bunch of code and nothing seemed to work. Initially I created a sitemap with another website and tried to upload it. I couldn't find where I should put the sitemap or what to configure to point CI to the sitemap. Ex. website.com/sitemap.xml. How do I go about doing that or should I use a script to generate the sitemap. I'm sorry if this is a really basic question!

Thanks,
BradM.
Did you see how PyroCMS does that?
Here is how I do it:

Before Starting here are some variables and considerations for this specific topic:
- I only have one dynamic pages called blog
- I have a few static pages

Step 1: Controller
I create a controller usually calling it seo.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class 
Seo extends CI_Controller {

 
   function sitemap()
 
   {
        
$this->load->model('blog_model');
        
$data['items'] = $this->blog_model->GetBlog(array(),'slug, created_on, tags');
 
       header("Content-Type: text/xml;charset=iso-8859-1");
 
       $this->load->view("sitemap",$data);
 
   }


Step 2: View
PHP Code:
<?php echo '<?xml version="1.0" encoding="UTF-8" ?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc><?php echo base_url();?></loc> 
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    <?php foreach($items as $item) { ?>
    <url>
        <loc><?php echo base_url().'post/'.$item->slug ?></loc>
        <priority>0.5</priority>
    </url>
    <?php ?>
    <url>
        <loc>http://www.examplesite.com/static-content</loc>
        <priority>0.5</priority>
    </url>
</urlset> 

Step 3: Route (config/routes.php)
In this step I just make sitemap.php be read as sitemap.xml
PHP Code:
$route['seo/sitemap\.xml'] = "seo/sitemap"
Try this one
https://github.com/o/sitemap-php

Works for me on CI3. I'm just convert this code to Library and use it on my project.
(07-13-2015, 12:24 AM)arma7x Wrote: [ -> ]Try this one
https://github.com/o/sitemap-php

Works for me on CI3. I'm just convert this code to Library and use it on my project.

could you send your library that you created starting from thet?
thanks
i'm using codeigniter with the sitemap php Library. Its awesome and has clear directions: https://github.com/evert/sitemap-php

To get you started - Put the SitemapPHP folder in application/libraries
Then, in your controller, in your method you call the library like:

 
PHP Code:
public function makeSitemap()
{

       // APPPATH will automatically figure out the correct path
       include APPPATH.'libraries/SitemapPHP/Sitemap.php';

       // your website url
       $sitemap = new Sitemap('http://yourwebsite.com');

       // This will also need to be set by you. 
       // the full server path to the sitemap folder 
       $sitemap->setPath('/home/username/public_html/sitemap/');

       // the name of the file that is being written to
       $sitemap->setFilename('mysitemap');

       // etc etc etc