phpyiiyii-routing

Yii Sending parameters to index action through page url


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 ?


Solution

  • 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.