魏长东

weichangdong

东邪

CI框架博客创建sitemap

    这里记录的是我这个博客创建sitemap的过程,是在SAE环境下。

    

新建三个文件:

一 application\controllers下的sitemap.php文件

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

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

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

 


 

二 application\models下的sitemapxml.php文件

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

	public function getArticle(){
		$this->db->select('pid,created,title');
		$this->db->where('type','post');
		$this->db->order_by('created', 'desc');
		$result=$this->db->get('posts');
		return $result->result_array();
	}

	public function getCategory(){
		$this->db->select('name');
		$this->db->where('type','category');
		$result=$this->db->get('metas');
		return $result->result_array();
	}
}

三 themes\default下的sitemap.php文件

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sitemap</title>
</head>
<body>
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
$xml = '<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL;
$xml .= '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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">'.PHP_EOL;

//首页单独写一个url
$xml .= '<url>'.PHP_EOL;
$xml .= ' <loc>'.'http://swordman.sinaapp.com'.'</loc>'.PHP_EOL;
$xml .= '<lastmod>'.date('Y-m-d',time()).'</lastmod>'.PHP_EOL;
$xml .= '<changefreq>'.'daily'.'</changefreq>'.PHP_EOL;
$xml .= '<priority>'.'1'.'</priority>'.PHP_EOL;
$xml .= '</url>'.PHP_EOL;

//类别页
foreach ($categorys as $category){
	$xml .= '<url>'.PHP_EOL;
	$xml .= ' <loc>'.'http://swordman.sinaapp.com/index.php/category/'
.$category['name'].'</loc>'.PHP_EOL;
	$xml .= '<lastmod>'.date('Y-m-d',time()).'</lastmod>'.PHP_EOL;
	$xml .= '<changefreq>'.'weekly'.'</changefreq>'.PHP_EOL;
	$xml .= '<priority>'.'0.8'.'</priority>'.PHP_EOL;
	$xml .= '</url>'.PHP_EOL;
}

//文章页
foreach ($posts as $post){
	$xml .= '<url>'.PHP_EOL;
	$xml .= ' <loc>'.'http://swordman.sinaapp.com/index.php/posts/'.$post['pid'].
'</loc>'.PHP_EOL;
	$xml .= '<lastmod>'.date('Y-m-d',$post['created']).'</lastmod>'.PHP_EOL;
	$xml .= '<changefreq>'.'weekly'.'</changefreq>'.PHP_EOL;
	$xml .= '<priority>'.'0.6'.'</priority>'.PHP_EOL;
	$xml .= '</url>'.PHP_EOL;
}

//留言板
/*
$xml .= '<url>').PHP_EOL;
$xml .= ' <loc>').'http://swordman.sinaapp.com/index.php/guest'.'</loc>').PHP_EOL;
$xml .= '<lastmod>').date('Y-m-d',time()).'</lastmod>').PHP_EOL;
$xml .= '<changefreq>').'weekly'.'</changefreq>').PHP_EOL;
$xml .= '<priority>').'0.5'.'</priority>').PHP_EOL;
$xml .= '</url>').PHP_EOL;
*/

$xml .= '</urlset>';
$domain = 'yourdomin';//Domain可以在后台面板设置
$url='saestor://yourdomin';
$filename="sitemap.xml";
$re=file_put_contents($url.$filename,$xml);
if ($re){
	echo 'ok';
} else {
	echo 'error';
}
?>
</body>
</html>

PS:因为我是直接写到SAE 的storage里面的,所以如果想生成到本地,把最后的代码改下,直接让echo就可以了。

访问http://swordman.sinaapp.com/index.php/sitemap/