简体   繁体   中英

Yii framework: Controller/Action url & parameters

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, ie I want my path to look like api/users/10 ?

Thank you!

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');

试试这个......

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

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM