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 ?

1
  • @David do you want to just send 123 without key? or you want to change the representation of Url? Commented Mar 8, 2014 at 15:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
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
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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.