thinkphp自带Page类使用时候setconfig() name=last的时候不生效

来源:互联网 发布:人生遥控器 知乎 编辑:程序博客网 时间:2024/05/21 17:41

问题:
在使用thinkphp自带分页类时,在设置尾页显示的最后一页时,用setConfig(“last”,”尾页”)来设置样式,发现无效。

分析:
在分页类(/ThinkPHP/Library/Think/Page.class.php)里面有一个共有属性:
public $lastSuffix = true; // 最后一页是否显示总页数
在show方法的实现中:
$this->lastSuffix && $this->config['last'] = $this->totalPages;
所以如果$lastSuffix = true的话,setConfig(‘last’,’尾页’)的设置会被重置,也就是说,我们的setConfig(“last”,”尾页”)被覆盖了。
解决方法:
1、我们可以在分页类里面直接修改属性:
public $lastSuffix = true;
但是,我们不知道类中在其他地方是否有使用到$lastSuffix这个变量,因此,为了不影响整个类,我们可以修改setConfig()方法

2、修改setConfig():
修改前:
public function setConfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
}

修改后:
public function setConfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name] = $value;
if($name == 'last'){
$this->lastSuffix = false;
}
}
}

好了,现在我们的setConfig(“last”,”尾页”);已经能够正常工作了。

0 0