CodeIgniter生成网站sitemap地图的方法

yipeiwu_com6年前PHP代码库

1.建立了一个名为sitemap的控制器

复制代码 代码如下:

<?php
if (!defined('BASEPATH'))
 exit ('No direct script access allowed');

class Sitemap extends CI_Controller{
 public function __construct() {
  parent::__construct();
  $this->load->model('sitemapxml'); 
 }

 function index(){
  $data['posts']=$this->sitemapxml->getArticle();
  $data['categorys']=$this->sitemapxml->getCategory();
  $this->load->view('sitemap.php',$data);
 }
}

首先加载sitemapxml模型类,index方法调用两个方法,分别获取文章列表和类别列表,以在模板中输出。

2.创建一个名为sitemapxml的模型

复制代码 代码如下:

<?php
class Sitemapxml extends CI_Model{
 public function __construct() {
  parent :: __construct();
  $this->load->database();
 }

 public function getArticle(){
  $this->db->select('ID,post_date,post_name');
  $this->db->order_by('post_date', 'desc');
  $result=$this->db->get('posts');
  return $result->result_array();
 }

 public function getCategory(){
  $this->db->select('c_sname');
  $result=$this->db->get('category');
  return $result->result_array();
 }
}

模型里面定义两个方法,获取文章列表和类别列表。

3.创建一个名为sitemap.php的模板

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sitemap</title>
</head>
<body>
<?php
echo htmlspecialchars('<?xml version="1.0" encoding="utf-8"?>').'<br/>';
echo htmlspecialchars('<urlset>').'<br/>';

//首页单独写一个url
echo htmlspecialchars('<url>').'<br/>';
echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com'.htmlspecialchars('</loc>').'<br/>';
echo htmlspecialchars('<lastmod>').date('Y-m-d',time()).htmlspecialchars('</lastmod>').'<br/>';
echo htmlspecialchars('<changefreq>').'daily'.htmlspecialchars('</changefreq>').'<br/>';
echo htmlspecialchars('<priority>').'1'.htmlspecialchars('</priority>').'<br/>';
echo htmlspecialchars('</url>').'<br/>';

//类别页
foreach ($categorys as $category){
 echo htmlspecialchars('<url>').'<br/>';
 echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com/index.php/home/cat/'.$category['c_sname'].htmlspecialchars('</loc>').'<br/>';
 echo htmlspecialchars('<lastmod>').date('Y-m-d',time()).htmlspecialchars('</lastmod>').'<br/>';
 echo htmlspecialchars('<changefreq>').'weekly'.htmlspecialchars('</changefreq>').'<br/>';
 echo htmlspecialchars('<priority>').'0.8'.htmlspecialchars('</priority>').'<br/>';
 echo htmlspecialchars('</url>').'<br/>';
}

//文章页
foreach ($posts as $post){
 echo htmlspecialchars('<url>').'<br/>';
 echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com/index.php/home/details/'.$post['post_name'].htmlspecialchars('</loc>').'<br/>';
 echo htmlspecialchars('<lastmod>').date('Y-m-d',strtotime($post['post_date'])).htmlspecialchars('</lastmod>').'<br/>';
 echo htmlspecialchars('<changefreq>').'weekly'.htmlspecialchars('</changefreq>').'<br/>';
 echo htmlspecialchars('<priority>').'0.6'.htmlspecialchars('</priority>').'<br/>';
 echo htmlspecialchars('</url>').'<br/>';
}

//留言板
echo htmlspecialchars('<url>').'<br/>';
echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com/index.php/guest'.htmlspecialchars('</loc>').'<br/>';
echo htmlspecialchars('<lastmod>').date('Y-m-d',time()).htmlspecialchars('</lastmod>').'<br/>';
echo htmlspecialchars('<changefreq>').'weekly'.htmlspecialchars('</changefreq>').'<br/>';
echo htmlspecialchars('<priority>').'0.5'.htmlspecialchars('</priority>').'<br/>';
echo htmlspecialchars('</url>').'<br/>';

echo htmlspecialchars('</urlset>');
?>
</body>
</html>

最重要的就是这个模板了,按照sitemap.xml的标准格式,从数据库中读取相关数据,用循环的方式自动生成这样的格式,页面上展示的是html形式的xml的内容。

然后再用一个很笨的方法,将生成的html文本(实际上就是xml文件的显示内容),复制到一个新建的sitemap.xml文件,格式化一下,保存,就产生了一个标准的sitemap.xml文件。因为要用的SAE部署应用,目录不支持写操作,只能这样上传了,隔一段时间这样弄一下就ok了。

相关文章

PHP构造函数与析构函数用法示例

本文实例讲述了PHP构造函数与析构函数用法。分享给大家供大家参考,具体如下: 在实例化一个新对象时,构造方法和析构方法都会被自动调用,若有继承则会使用父类的对应方法。 析构方法在三种情况...

9个PHP开发常用功能函数小结

1. 函数的任意数目的参数   你可能知道PHP允许你定义一个默认参数的函数。但你可能并不知道PHP还允许你定义一个完全任意的参数的函数   下面是一个示例向你展示了默认参数的函数: 复...

详解PHP神奇又有用的Trait

php和java,c++一样都是单继承模式。但是像python,是支持多继承(即Mixin模式)。那么如何在php中实现多继承模式?这就需要使用trait。trait Array...

php代码审计比较有意思的例子

php代码审计比较有意思的例子

代码审计比较有意思的例子貌似是去年 ecshop支付漏洞偶然出来的一个例子,感觉不错。分享下 复制代码 代码如下:<?php$a=addslashes($_GET['a']);$b...

PHP 常用的header头部定义汇总

header() 函数向客户端发送原始的 HTTP 报头。 认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用...