2

How do I set the route for our sitemap? Currently we set header in our controller, but in the view, it will show as plain text.

controller code

public function sitemap(){

 $data = [];
 $model = new BlogModel();
$data[‘blogs’] = $model->where(‘STATUS’, ‘1’)->orderBy(‘ID’, ‘DESC’)->findAll();

 return view(‘sitemap’, $data);
}

sitemap code

<?php echo ‘<?xml version=”1.0" encoding=”UTF-8"?>’; ?>
<urlset
 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
 xmlns:image=”http://www.google.com/schemas/sitemap-image/1.1"
 xsi:schemaLocation=”http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
 xmlns=”http://www.sitemaps.org/schemas/sitemap/0.9">
 <! — created with Free Online Sitemap Generator www.xml-sitemaps.com →

 <url>
 <loc><?= base_url();?></loc>
 <changefreq>daily</changefreq>
 <priority>1.0</priority>
 </url>
<?php foreach($blogs as $blog){
$cat_id = $blog[‘PAGE_ID’];
 $db = \Config\Database::connect();
 $query = $db->query(“SELECT * from mcms_links WHERE ID = ‘$cat_id’”);
 $result = $query->getRowArray();
?>
<url>
<loc><?=base_url();?>/<?=$result[‘VALID_NAME’];?>/<?=$blog[‘VALID_NAME’];?></loc>
<changefreq>daily</changefreq>
<priority>1.00</priority>
</url>
<?php }
?>
</urlset>

routes

$routes->get(‘sitemap\.xml’, ‘Sitemap::sitemap’);
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • 1
    please read my answer [here](https://stackoverflow.com/questions/65482349/create-a-sitemap-xml-programmatically-for-a-multi-language-multi-domain-site-o) and adapt, if necessary for CI 4.x – Vickel Jul 01 '22 at 11:32
  • quotation marks are inconsistent - " and ” are different utf8 characters. not sure if both works, but for sure it will break the whole code if you are mixing them. also in routes ‘ needs to be ' or " can you please fix them and tell us if you still don't get the desired outcome? – Tsefo Sep 05 '22 at 20:47

0 Answers0