修改YII的请求参数

在YII2中,由于封装了大量的插件,很多插件是依赖Yii::$app->request->queryParams这样的参数,如Sort排序,Linkpager分页、Gridview表格等,因此有时因需要是有必要修改该参数。由于queryParams是只读属性,不能直接修改。但它提供了显式的setQueryParams的函数,让你修改。如下所示
$searchModel = new BookSearch();
$params = Yii::$app->request->queryParams;
if(isset($params['sort'])){
$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie([
'name' => $this->id.'_index_sort',
'value' => $params['sort'],
'expire'=>time()+3600
]));
}else{
$cookies = Yii::$app->request->cookies;
$sort = $cookies->get($this->id.'_index_sort', '');
$params['sort'] = $sort->value;
Yii::$app->request->setQueryParams($params);
}
$dataProvider = $searchModel->search($params);

id