If you want the path to be formed exactly in the browser line, use redirect:
return $this->redirect(['user/index', 'id' => 'mainpage', 'user' => 1]);
TL; DR;
If you are interested in the default settings, you can do this either through routing or directly in the controller.
Default parameters via routing
In the application config:
$config = [ 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ // other rules ['verb' => 'GET', 'pattern' => 'user/index', 'route' => 'user/index', 'defaults' => ['id' => 'mainpage', 'user' => 1]], ] ] ], ];
The default settings in the controller
Let me assume that $currentUser
should still be searched by the parameter $user
$input = DynamicModel::validateData([ 'id' => \Yii::$app->request->get('id', 'mainpage'), // вторым параметром -- значение по-умолчанию 'user' => \Yii::$app->request->get('user', \Yii::$app->user->isGuest ? '1' : \Yii::$app->user->identity->nick_name), // тут вторым параметром идет значение по-умолчанию, которое зависит от условия // через routing так сделать не получится ], [ ['user', 'exist', 'targetClass' => User::className(), 'targetAttribute' => 'nick_name'], // можно добавить другие валидаторы // http://www.yiiframework.com/doc-2.0/guide-input-validation.html#ad-hoc-validation ]); if ($input->hasErrors()) { // что-нибудь на случай ошибки, например redirect на 404 } return $this->render('index', [ 'currentUser' => User::findOne(['nick_name' => $input->user]), 'id' => $input->id // это как я понимаю page ID, так что здесь скорее будет что-то типа Page::findOne($input->id) // ну и соответствующий валидатор тогда нужен выше ]);
I would certainly recommend the second option, since here it is possible to customize the default settings more flexibly.
I also want to note that in your code there are many ... oddities:
- The
user
parameter in the controller is not used at all (unless you are processing it somehow in the routing, which is unlikely) currentUser
is searched by parameter id
which semantically means nick_name
- not an authorized user can go to the page with any
id
, and an authorized user can only go to his - if an incorrect
id
passed to an unauthorized user, the view with $currentUser === null
displayed (for an authorized user, this situation will always be if $id
not empty and is not equal nick_name
current user nick_name
) - unauthorized users are allowed to search
$currentUser
via HTTP only method GET
, authorized - using any HTTP method
Some of these oddities are fixed in the example above.
http://user/index?id=mainpage&user=1
with his data should be formed - Maybe_V