php - How to create dynamic sitemap in Codeigniter
问题描述
How to create a dynamic sitemap in Codeigniter. I have created a php file in Controller named it Sitemap.php
and created a view named it sitemap.php
everything is going well but showing the following result not the actual XML file as shown in below image.
Codeigniter Sitemap.php file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sitemap extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('Constant_model');
$this->load->helper('xml');
}
public function index()
{
$query= $this->db->query("SELECT url_slug FROM snippets UNION SELECT tag_name FROM tags");
$data = $query->result();
$data['items'] =$data;
$this->load->view('sitemap', $data);
}
}
View -> sitemap.php file
<?php echo'<?xml version="1.0" encoding="UTF-8" ?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc><?php echo base_url();?></loc>
<priority>1.0</priority>
<changefreq>daily</changefreq>
</url>
<!-- Sitemap -->
<?php foreach($items as $item) { ?>
<url>
<loc><?php echo base_url()."item/".$item->url_slug ?></loc>
<priority>0.5</priority>
<changefreq>daily</changefreq>
</url>
<?php } ?>
解决方案
您缺少header
控制器中的设置,请像这样放置标题
Class Sitemap extends CI_Controller {
function sitemap()
{
$query= $this->db->query("SELECT url_slug FROM snippets UNION SELECT tag_name FROM tags");
$data = $query->result();
$data['items'] =$data;
header("Content-Type: text/xml;charset=iso-8859-1");
$this->load->view('sitemap', $data);
}
}
您的视图文件似乎没问题。
有关更多详细信息,请参阅此链接
推荐阅读
- python - Python CV2 选择掩码的 alpha 使其透明
- python - 在 zip 函数之前使用星号
- python-3.x - 使用数据流在 gcs 存储桶上按大小列出文件夹
- python - 获取单字段 TicDat 表值的最快方法
- ruby-on-rails-3 - 预期 ActiveRecord::RecordNotFound 但没有提出任何内容
- python - 蟒蛇 | 连接两个不同大小的字符串数组并保留匹配
- python - 如何添加允许用户添加数字的 GUI,然后单击排序将对代码进行排序
- reactjs - redux 发送响应和返回消息
- java - 调用 jpa 存储库中的存储过程时出现错误位置参数 [1] 未在此过程调用中注册
- ruby - 变量声明中字符串、其他变量和正则表达式的组合