计算机网络/计算机科学与应用/系统/运维/开发

TP3.2.3路由使用

通常tp自带pathinfo路由模式为 http://域名/team/index.html ,

详情页:http://域名/Team/show/id/115.html,

如果我们想美化url地址,那可以使用tp自带的路由功能


第一步:首先在模块中开启路由,配置路由规则

return array(
    //'配置项'=>'配置值'
    //开启路由
    'URL_ROUTER_ON'=>true,
    //定义路由规则
    'URL_ROUTE_RULES'=>array(
        'article/:a_id\d'=>'Article/show',
        'article'     => 'Article/news',
    ),
);

配置路由后,我们就可以通过 http://域名/article.html  和  http://域名/article/188.html 进行访问,

那么如果是volist列表进行循环,该怎么进行配置呢?这时候就需要用到tp U方法


<volist name="white" id="vo">
            <a href="{:U('article/'.$vo['a_id'])}" class="decknow_li f_cb">
                <div class="f_fl"><img src="__ROOT__{$vo.photo}"/></div>
                <div class="article f_fl">
                    <div class="f_cb">
                        <h3 class="f_fl">{$vo.cate_name}:{$vo.a_title}</h3><span class="f_fr">{$vo.create_time|date='Y-m-d H:i',###}</span>
                    </div>
                    <p>{$vo.a_remark}
                    </p>
                    <span class="view_more">了解详情</span>
                </div>
            </a>
</volist>

列表页,和文章页都做好路由了,那么如何分页呢?


首先要设置一下tp自带分页类


打开ThinkPHP\Library\Think 找到 __construct方法 添加一个参数$url=''


   public function __construct($totalRows, $listRows=20, $parameter = array(),$url='') {
        C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
        /* 基础设置 */
        $this->totalRows  = $totalRows; //设置总记录数
        $this->listRows   = $listRows;  //设置每页显示行数
        $this->parameter  = empty($parameter) ? $_GET : $parameter;
        $this->nowPage    = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
        $this->nowPage    = $this->nowPage>0 ? $this->nowPage : 1;
        $this->firstRow   = $this->listRows * ($this->nowPage - 1);
        $this->url=$url;
    }

将show方法

$this->parameter[$this->p] = '[PAGE]';
$this->url = U(ACTION_NAME, $this->parameter);

修改为:

public function show() {
        if(0 == $this->totalRows) return '';
        /* 生成URL */
        // $this->parameter[$this->p] = '[PAGE]';
        // $this->url = U(ACTION_NAME, $this->parameter);
        if(empty($this->url)){
            $this->parameter[$this->p] = '[PAGE]';
            $this->url = U(ACTION_NAME, $this->parameter);
        }else{
            $depr = C('URL_PATHINFO_DEPR');
            $this->url = rtrim(U('/'.$this->url,'',false),$depr).$depr.urlencode('[PAGE]');
        }


实例化代码


 $count= $article->where('web_article.cate_id=3')->count(); // 查询满足要求的总记录数
            $Page       = new \Think\Page($count,5,'','article/p');// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page -> setConfig('header', '<a class="page_count">&nbsp;%NOW_PAGE%/共%TOTAL_PAGE%页</a>');
            $Page -> setConfig('prev', '上一页');
            $Page -> setConfig('next', '下一页');
            $Page -> setConfig('last', '末页');
            $Page -> setConfig('first', '首页');
            $Page -> setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%');
            $Page -> lastSuffix = false;
            $show = $Page->show();// 分页显示输出


配置路由规则

'page/:p\d'=>'Home/Index/index',

参考文章:https://jingyan.baidu.com/article/63f23628346c0e0209ab3d6e.html


tp3.2.3

若无人替你披荆斩棘,那就独自前行栉风沐雨。

评论

^