Yii framework: Controller/Action url & parameters

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

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!

回答1:

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


回答2:

Try This......

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


回答3:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!