We create a dynamic sitemap in CodeIgniter but in view it will treat my XML file as HTML

Viewed 91

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’);
0 Answers
Related