1

I am trying to create sitemap in my codeigniter app following this answer

Here is my controller method:

public function siteMap() {

    $this->load->helper('url');

    $urls = array("test");

    $data['urls'] = $urls;
    $data['frontend'] = $this->getFronendItems();

    $this->load->template('front/site_map.php', $data);

}

And my view:

<?php header('Content-type: text/xml'); ?>
<?= '<?xml version="1.0" encoding="UTF-8" ?>' ?>

<url>
    <loc><?= base_url() ?></loc> 
    <priority>1.0</priority>
</url>

<?php foreach($urls as $url) { ?>
<url>
    <loc><?= base_url() . $url ?></loc>
    <priority>0.5</priority>
</url>
<?php } ?>

This raises the following error:

This page contains the following errors:

error on line 41 at column 8: Opening and ending tag mismatch: link line 0 and head

Tried to remove the header and the url is just echoing as a string on the screen. What am I doing wrong ?

Community
  • 1
  • 1
Alexander Nikolov
  • 1,871
  • 7
  • 27
  • 49

1 Answers1

0

You should always place a header above any view files, usually I place mine at the top of a controller method.

public function siteMap() {
   header("Content-Type: text/xml;charset=iso-8859-1");

    $this->load->helper('url');

    $urls = array("test");

    $data['urls'] = $urls;
    $data['frontend'] = $this->getFronendItems();

    $this->load->template('front/site_map.php', $data);

}

in the view file, add

<?php print '<?xml version="1.0" encoding="utf-8"?>';?>
<?php print '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';?>

<url>
  <loc><?=base_url()?></loc> 
  <priority>1.0</priority>
</url>

<?php foreach($urls as $url) {
    ?>
    <url>
      <loc><?=base_url().$url?></loc>
      <priority>0.5</priority>
    </url>
    <?php 
} 
?>

<?php print '</urlset>';?>

This is exactly how I do my sitemap.

tmarois
  • 2,424
  • 2
  • 31
  • 43