I
I
Ivan Ivanov2021-01-22 22:36:13
Yii
Ivan Ivanov, 2021-01-22 22:36:13

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')...
}

Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-01-23
@maksim_fix

1. Through Request

if(!\Yii::$app->request->get('sort')) {
   Yii::$app->request->setQueryParams(['sort' => 'name']);
}

2. Through a redirect
if(!\Yii::$app->request->get('sort')) {
    $this->redirect(['view', 'sort' => 'name']);
}

3. In DataProvider
$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'sort' => [
        'defaultOrder' => [
            'name' => SORT_ASC
        ]
    ],
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question