0
mysite/users triggers usersController index action.

Now I want to send mysite/users/123 to usersController.

How Can I do it ? and where to will go this request if not to index action ?

David
  • 4,332
  • 13
  • 54
  • 93

1 Answers1

1

Updated form comments discussion

If you want to pass 123 to the controller you need to set the named parameter in the urlManager of the config/main.php file.

'something/<namedParameter:Pattern>' => 'MyControllerToCall/MyMethodofControllerToUse'

So for you:

'users/<username:\d+>' => 'users/view'

In the view

public function actionView($username){

echo $username;
}

That will only work in the view method though as the 123 part is being taken from the URL and you have configured Yii so when the url mysite/users/ is displayed like mysite/users/123. It calls the View method of the users Controller.

Jonnny
  • 4,939
  • 11
  • 63
  • 93
  • so If I write in config 'users'=>'usersController/view' Then in controllers file I have public function actionView($username){ and this '123' will go to $username variable ? – David Mar 08 '14 at 16:03
  • It needs to be `'users' => 'users/view'`. Then in that will call the `users` Controller and the `view` method. But no, it won't do that because you haven't passed anything to the left hand side of the rule. The other I answer I gave you will do what you ask. But that was for the url `users/123`. This rule you said `mysite/users`. But, normally I would say if you are having a url like `mysite/user`, then it would call the index (`users/index`) method and get all users. Then the url `users/123` is more specific and would call just the user with the username 123 – Jonnny Mar 08 '14 at 16:08
  • Please forget my other question, there the story is a bit different from this. And here what I want to do is to pass this '123' data to any action of usersController, I want to trigger the controller and to test it I want to echo right there in action this '123' – David Mar 08 '14 at 16:13