How to create a sitemap in codeigniter?

Member

by jerad , in category: PHP CMS , 2 months ago

How to create a sitemap in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , 2 months ago

@jerad 

In CodeIgniter, you can create a sitemap by following these steps:

  1. Create a new controller called Sitemap.php in your controllers folder.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sitemap extends CI_Controller {

    public function index()
    {
        $data = array(
            'urls' => array(
                site_url('home'),
                site_url('about'),
                // Add more URLs here
            )
        );

        $this->output->set_output($this->load->view('sitemap', $data, TRUE));
    }

}


  1. Create a new view file called sitemap.php in your views folder.
1
2
3
4
5
6
7
8
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <?php foreach ($urls as $url): ?>
        <url>
            <loc><?php echo $url; ?></loc>
        </url>
    <?php endforeach; ?>
</urlset>


  1. Add a new route in your routes.php file to map the /sitemap.xml URL to the Sitemap controller.
1
$route['sitemap.xml'] = 'sitemap';


  1. Access the sitemap by visiting /sitemap.xml in your browser.


This is a basic example of how to create a sitemap in CodeIgniter. You can customize the URLs in the sitemap.php view file and add any additional functionality as needed.