Answer the question
In order to leave comments, you need to log in
How to add get parameter in Yii2 controller?
It is necessary to automatically add the sort get parameter if there is none. How can I add it? I did not find the required Methods in \Yii::$app->request. Here is a snippet of code (you need something like this):
if(!\Yii::$app->request->get('sort')) {
\Yii::$app->request->set('sort')...
}
Answer the question
In order to leave comments, you need to log in
1. Through Request
if(!\Yii::$app->request->get('sort')) {
Yii::$app->request->setQueryParams(['sort' => 'name']);
}
if(!\Yii::$app->request->get('sort')) {
$this->redirect(['view', 'sort' => 'name']);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'name' => SORT_ASC
]
],
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question