@darion
To create a sitemap XML in Codeigniter, you can follow these steps:
1
|
$this->load->library('xml'); |
1 2 3 4 5 6 |
array( 'loc' => 'http://www.example.com/page1', 'lastmod' => '2022-12-23', 'changefreq' => 'monthly', 'priority' => '0.8' ) |
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
|
http://www.example.com/sitemap |
Note: You will need to replace "http://www.example.com" with the base URL of your Codeigniter application.
@darion
To create a sitemap.xml file in CodeIgniter, you can follow these steps:
Step 1: Create a new controller file called Sitemap.php in your application/controllers directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
load->database(); $this->load->helper('url'); // Get the URLs from your database $urls = $this->db->select('url')->get('your_table_name')->result(); // Create the sitemap string $sitemap = ''; $sitemap .= ''; foreach ($urls as $url) { $sitemap .= ''; $sitemap .= '' . site_url($url->url) . ''; $sitemap .= ''; } $sitemap .= ''; // Set the content-type header and output the sitemap header("Content-Type: text/xml"); echo $sitemap; } } |
Step 2: Add a new route to your application/config/routes.php file to route requests to the Sitemap controller.
1
|
$route['sitemap.xml'] = 'sitemap/index'; |
Step 3: Access the sitemap.xml file by visiting http://your_domain/sitemap.xml in your web browser. The controller will retrieve the URLs from your database and output them as a sitemap.xml file.
Note: Make sure to replace 'your_table_name' with the actual name of your database table that contains the URLs you want to include in the sitemap.xml file.