如何在thinkphp3.0中实现分页的伪静态代码

前阵子我做的一个简单作品“写给未来的自己”使用了Thinkphp3.0版本的框架(SAE版本, 不过和普通的用起来没啥区别), 但是Tp3.0中对于分页没有提供伪静态的支持, 也就是说, 翻页的链接是”http://abc.com/a/b/c?&p=1″这样的形式, 而无法使用成类似于”http://abc.com/a/b/c/p/1″这样的.

从这个实现本身来看没啥问题, 只不过是显示起来比较别扭. 但是实际上, 这个对于搜索引擎的抓取非常不利. 比如我在百度和google分别提交了sitemap, google还好, &p=1和&p=2这样子的都可以当做不同的网页抓取, 在搜索框中用”site: hi2future.com”可以看到基本上所有的页面都是可以检索到的. 但是百度弱爆了, 这种&p=1的都被认为实际调用的页面是相同一个(当然, 实际的确是这样…), 结果很多分页的url地址就无法被索引到.

大怒之下, 就改写了tp3.0的分页类.

未修改的php文件直接下载: Page.class
修改后的php文件直接下载: Page.class

我修改的原则是,
1. 直接基于原有代码进行修改, 不重新写
2. 兼容各种情况:
1) http://abc.com/p/1(正常url)
2) http://abc.com/a/1(不包含分页参数)
3) http://abc.com/a/1/p/2/x/3(分页参数的后面还有其他的参数以tp默认的方式传入)
4) http://abc.com/a/1/p/2/x/3?abc=xyz(同上, 不过后面还有以问号分隔的正常url get参数传递)
5) http://abc.com/a/1/p/32/..(页码参数不仅仅有一位数字)
6) http://abc.com/a/1/p/2?abc=xyz(分页参数后面直接跟着?的正常参数)
3. 分页的地址可以配置, 如http://abc.com/a/1/page/3这样

基于以上一些原则, 直接给出修改后的代码如下:

<?php
// +----------------------------------------------------------------------
// ¦ ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// ¦ Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// ¦ Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// ¦ Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: Page.class.php 2712 2012-02-06 10:12:49Z liu21st $

class Page {
    // 分页栏每页显示的页数
    public $rollPage = 5;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow	;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  =	array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
    // 默认分页变量名
    protected $varPage;

    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($totalRows,$listRows='',$parameter='') {
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        //$this->varPage = 'p' ;
		$this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }

    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }

    /**
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function show() {
        if(0 == $this->totalRows) return '';
        $p = $this->varPage;
        $nowCoolPage      = ceil($this->nowPage/$this->rollPage);
        $ori_url = $_SERVER['REQUEST_URI'];
        //类似于http://abc.com/a/1/p/3/x/9?abc=xyz,这样的形式,需要保留参数a,x,abc,此处仅仅需要根据/p/3生成/p/其他页
        $url_page_index = stripos($ori_url, '/'.$p.'/');
        $url_length = strlen($ori_url);
        //如果找不到/p/3,则在?前面添加p的参数,即将url以?分割成两个部分
        if ($url_page_index == false)
        {
        	$url_wen_index = stripos($ori_url, '?');
        	if ($url_wen_index == false)
        	{
        		$url_prefix = $ori_url;
        		$url_suffix = "";
        	}
        	else 
        	{
        		$url_prefix = substr($ori_url, 0, $url_wen_index);
        		$url_suffix = substr($ori_url, $url_wen_index, $url_length - $url_wen_index);
        	}
        }
        //否则,保留/p/3前后的url信息,用于拼装
        else
        {
        	$url_page_index_last = $url_page_index + strlen($p) + 2;
        	while ($ori_url[$url_page_index_last] >= '0' 
        		&& $ori_url[$url_page_index_last] <= '9' 
        		&& $url_page_index_last < $url_length - 1)
        	{
        		$url_page_index_last ++;
        	}
        	$url_page_index_last ++;
        	$url_prefix = substr($ori_url, 0, $url_page_index);
        	if ($url_page_index_last == $url_length)
        	{
        		$url_suffix = "";
        	}
        	else
        	{
        		$url_suffix = substr($ori_url, $url_page_index_last - 1, $url_length - $url_page_index_last + 1);
        	}
        }

//        var_dump($url_prefix);
//        var_dump($url_suffix);

        //上下翻页字符串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        if ($upRow>0){
            $upPage="<a href='".$url_prefix."/".$p."/$upRow".$url_suffix."'>".$this->config['prev']."</a>";
        }else{
            $upPage="";
        }

        if ($downRow <= $this->totalPages){
            $downPage="<a href='".$url_prefix."/".$p."/$downRow".$url_suffix."'>".$this->config['next']."</a>";
        }else{
            $downPage="";
        }
        // << < > >>
        if($nowCoolPage == 1){
            $theFirst = "";
            $prePage = "";
        }else{
            $preRow =  $this->nowPage-$this->rollPage;
            $prePage = "<a href='".$url_prefix."/".$p."/$preRow".$url_suffix."' >上".$this->rollPage."页</a>";
            $theFirst = "<a href='".$url_prefix."/".$p."/1".$url_suffix."' >".$this->config['first']."</a>";
        }
        if($nowCoolPage == $this->coolPages){
            $nextPage = "";
            $theEnd="";
        }else{
            $nextRow = $this->nowPage+$this->rollPage;
            $theEndRow = $this->totalPages;
            $nextPage = "<a href='".$url_prefix."/".$p."/$nextRow".$url_suffix."' >下".$this->rollPage."页</a>";
            $theEnd = "<a href='".$url_prefix."/".$p."/$theEndRow".$url_suffix."' >".$this->config['last']."</a>";
        }
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$i<=$this->rollPage;$i++){
            $page=($nowCoolPage-1)*$this->rollPage+$i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    $linkPage .= "&nbsp;<a href='".$url_prefix."/".$p."/$page".$url_suffix."'>&nbsp;".$page."&nbsp;</a>";
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "&nbsp;<span class='current'>".$page."</span>";
                }
            }
        }
        $pageStr	 =	 str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
        return $pageStr;
    }

}

//$request_urls = array(
//	"http://localhost/a/1/p/2",
//	"http://localhost/a/1/p/25",
//	"http://localhost/a/1/p/3/x/3",
//	"http://localhost/a/1/p/32/x/3",
//	"http://localhost/a/1",
//	"http://localhost/a/1?abc=xyz",
//	"http://localhost/a/1/p/3?abc=xyz",
//	"http://localhost/a/1/p/3/x/3?abc=xyz",
//	"http://localhost/a/1/p/32?abc=xyz",
//	"http://localhost/a/1/p/32/x/3?abc=xyz",
//);
//
//foreach ($request_urls as $key=>$value)
//{
//	$_SERVER['REQUEST_URI'] = $value;
//	$page = new Page(16, 5);
//    $show = $page->show();
//    var_dump($show);
//}

其中测试的时候, 可以将50行先删掉, 使用第49行的代码, 将最后面的测试代码注释去掉, 直接当做php脚本来跑~

update(2013.02.17): 发现有个小bug。。

while ($ori_url[$url_page_index_last] >= ’0′ && $ori_url[$url_page_index_last] <= ’9′ && $url_page_index_last < $url_length – 1)

这个判断有误,原来少写了=,这会导致翻页翻到90页的时候,上一页的链接会变成8990(后面的90没有被去掉)

 


本文链接:https://www.poisonbian.com/post/347.html 转载需授权!

分享到:
原文链接:,转发请注明来源!
「如何在thinkphp3.0中实现分页的伪静态代码」评论列表

发表评论