How can you realize that when you switch to the controller without specifying any parameters, a certain path was formed!

For example, when a user enters http://user/index?id= or http://user/index? automatically formed the path http://user/index?id=mainpage&user=1

What means is implemented?

Act

 public function actionIndex() { $currentUser = null; if (Yii::$app->user->isGuest || Yii::$app->request->isGet) { if ($id = Yii::$app->request->get('id')) { $currentUser = User::find()->where(['nick_name' => $id])->one(); } } if (!Yii::$app->user->isGuest ) { if (Yii::$app->request->get('id') == Yii::$app->user->identity->nick_name || Yii::$app->request->get('id') == null ) { $currentUser = User::find()->where(['nick_name' => Yii::$app->user->identity->nick_name])->one(); } } return $this->render('index', [ 'currentUser' => $currentUser, ]); } 
  • Regardless of the parameters? Or specifically for the above conditions? Describe the specific task as a customer, and not as a programmer. May offer a better option - Urmuz Tagizade
  • @UrmuzTagizade, when a user clicks a link to his page in the query string, a line of the kind I gave in the example should be formed! But even when the user enters just the name of the controller plus the action, a line of the form http://user/index?id=mainpage&user=1 with his data should be formed - Maybe_V
  • throw off a piece of controller code with action - Urmuz Tagizade
  • @UrmuzTagizade, threw action - Maybe_V
  • Just redirect to another URL. in the controller action. - withoutname

1 answer 1

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:

  1. The user parameter in the controller is not used at all (unless you are processing it somehow in the routing, which is unlikely)
  2. currentUser is searched by parameter id which semantically means nick_name
  3. not an authorized user can go to the page with any id , and an authorized user can only go to his
  4. 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 )
  5. 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.