@jacey.lubowitz
To create a sitemap in CodeIgniter, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Sitemap extends CI_Controller { public function index() { // Load necessary helpers and libraries $this->load->helper('url'); $this->load->dbutil(); // Get all necessary data for sitemap $data['pages'] = $this->db->get('pages')->result(); $data['posts'] = $this->db->get('posts')->result(); // Create an XML sitemap string $sitemap = '<?xml version="1.0" encoding="UTF-8"?>'; $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; // Add URLs for pages foreach ($data['pages'] as $page) { $sitemap .= '<url>'; $sitemap .= '<loc>'.site_url($page->slug).'</loc>'; $sitemap .= '<lastmod>'.date("c", strtotime($page->updated_at)).'</lastmod>'; $sitemap .= '</url>'; } // Add URLs for posts foreach ($data['posts'] as $post) { $sitemap .= '<url>'; $sitemap .= '<loc>'.site_url('posts/'.$post->id).'</loc>'; $sitemap .= '<lastmod>'.date("c", strtotime($post->updated_at)).'</lastmod>'; $sitemap .= '</url>'; } $sitemap .= '</urlset>'; // Set the content-type header and output the sitemap header("Content-type: text/xml"); echo $sitemap; } } |
Note: You may need to modify the database table names and column names according to your project's structure. Additionally, make sure your routes are configured properly to access the Sitemap controller.