用CodeIgniter建立網站sitemap地圖

來源:互聯網
上載者:User

本人用CodeIgniter建立了一個小型網站:http://101ban.sinaapp.com/,為了更好的搜尋引擎收錄,有必要給網站產生一個xml格式的sitemap地圖,但是上網找了很多資料,都沒發現好的教程,或者功能太過於繁雜。於是自己琢磨弄了一個簡單的適合自己的方法,下面分享一下這個方法。

說明:在CodeIgniter環境下開發的程式,放到SAE運行。

1.建立了一個名為sitemap的控制器

<?phpif (!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的模型

 

<?phpclass 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><?phpecho htmlspecialchars('<?xml version="1.0" encoding="utf-8"?>').'<br/>';echo htmlspecialchars('<urlset>').'<br/>'; //首頁單獨寫一個urlecho htmlspecialchars('<url>').'<br/>';echo htmlspecialchars(' <loc>').'http://101ban.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://101ban.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://101ban.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://101ban.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了。

附上已經做好的地址,可以開啟看看效果。http://101ban.sinaapp.com/index.php/sitemap

關於sitemap.xml的標準格式,請參加本站另一篇文章:http://tanteng.sinaapp.com/2013/04/sitemap/

(小談部落格原創,轉載請註明出處!http://tanteng.sinaapp.com/2013/04/codeigniter-create-sitemap/)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.