18

In my application , I have ApiController with actionUsers, So in YII the path becomes api/users. Now in order to get certain users info , I use the following path api/users/id/10 where 10 is the userID and id part of the path is basically a GET parameter (api/users?id=10).

Is there any way to do the same thing without id part of the path, i.e. I want my path to look like api/users/10?

Thank you!

3 Answers 3

23

You're going to need to put in rule patterns in the urlManager component:

Yii Framework Documentation: url

Your config should look something like this:

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'api/users/<id>'=>'api/users',
            ),
        ),
    ),
);

You can then get the value by:

$id = Yii::app()->getRequest()->getQuery('id');
Sign up to request clarification or add additional context in comments.

1 Comment

It's a shame that Yii doesn't do this mapping for you implicitly. For 95% of cases, /users/view/1/ should go to UserController::view($id) without you having to instruct Yii to make this connection.
3

Try This......

$id = Yii::app()->request->getParam('id');

1 Comment

can it be done like if user pass id then also it should go to the action and if not then also in the same action?
0

in addition to @shiki's answer you can also do this

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'api/users/<id>'=>'api/users',
            ),
        ),
    ),
);

and in action

public function actionUsers($id=null)  // argument variable should same as in urlmanager
    {
     echo $id;
    }

Comments

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.