Yii 1分页使用用户插入的每个参数

gorkyyrv  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(100)

我将我的分页URL设置为www... com/category/detail.html?page=2
我的代码是,

$dataProvider = new CActiveDataProvider('Page', array('criteria' => array('condition' => 'status=1', 'condition' => 'category_id=' . $categoryObject -> id, 'order' => 'postDate DESC'), 'pagination' => array('pageSize' => 4,'pageVar'=>'page'), ));
$dataProvider->getData();
var_dump $dataProvider->totalItemCount;

我正在获取准确的数据计数,并且我的分页URL似乎可以正常工作。我将URL规则配置为

'index'=>'site/index',
'contact'=>'site/contact',
'privacy'=>'site/privacy',
'sitemap.xml'=>'site/sitemap',
'<category:\w+>' => 'category/detail',
'<category>/page/<page:\d+>' => 'category/detail',
'<category>'=>'category/detail',
'<category:\w+>/<postTitle:.+>' => 'category/post',
'<category:\w.+>/<postTitle:.+>'=>'category/post',

我的自动生成的分页网址是工作正常,但如果我手动插入网址的东西,如www...... com/category/detail.html?Page_page&page=2 www...... com/category/detail.html?Page_pizza&page=2
或者我想把这些额外的参数去掉,或者我想把我的分页网址严格限制在www... com/category/detail.html?page=2,如果我放了任何额外的参数,我想看到一个错误页面。
我已经在这工作了2个星期,并尝试了一切可能的方法,我可以。

drnojrws

drnojrws1#

您应该将分页的“params”属性设定为空数组:

'pagination' => array('pageSize' => 4,'pageVar'=>'page', 'params' => array()),

这将避免从分页链接中排除任何GET参数(如果需要包含任何GET参数,请在此处添加它们的名称)。
若要掷回错误,您需要检查动作中的参数。

// remove valid prameters from get array.
$arr = array_diff_key($_GET, array_flip(array('page', 'postTitle', 'category')));
// if still there are parameters left, throw an error.
if(count($arr) > 0) {
    throw new CHttpException('invalid parameter');
}

相关问题