How to create sitemap xml in Codeigniter?

Member

by darion , in category: PHP Frameworks , 8 months ago

How to create sitemap xml in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , 3 months ago

@darion 

To create a sitemap XML in Codeigniter, you can follow these steps:

  1. Create a new controller called "Sitemap" in your application/controllers directory.
  2. In the Sitemap controller, create a function called "index" which will be used to generate the sitemap XML.
  3. In the index function, load the Codeigniter XML library by adding the following line at the top of the function:
1
$this->load->library('xml');


  1. Create an array of URLs that you want to include in the sitemap. Each element of the array should have the following structure:
1
2
3
4
5
6
array(
    'loc' => 'http://www.example.com/page1',
    'lastmod' => '2022-12-23',
    'changefreq' => 'monthly',
    'priority' => '0.8'
)


  1. Use the Codeigniter XML library to generate the sitemap XML. To do this, add the following lines of code to your index function:
1
2
3
4
5
6
7
$data = array(
    'urls' => $urls
);
$this->xml->add_root('urlset');
$this->xml->add_child($data);
$this->output->set_header('Content-Type: application/xml');
$this->output->set_output($this->xml->get_xml());


  1. Save the Sitemap controller and visit the following URL in your browser to see the generated sitemap XML:
1
http://www.example.com/sitemap


Note: You will need to replace "http://www.example.com" with the base URL of your Codeigniter application.