php Yii 框架:控制器/动作 url 和参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2760869/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii framework: Controller/Action url & parameters
提问by Peterim
In my application , I have ApiControllerwith 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/10where 10 is the userID and idpart of the path is basically a GET parameter (api/users?id=10).
在我的应用程序,我ApiController用actionUsers,所以在YII的路径变得api/users。现在为了获取某些用户信息,我使用以下路径api/users/id/10,其中 10 是用户 ID,id路径的一部分基本上是 GET 参数 ( api/users?id=10)。
Is there any way to do the same thing without idpart of the path, i.e. I want my path to look like api/users/10?
有没有办法在没有id路径的情况下做同样的事情,即我希望我的路径看起来像api/users/10?
Thank you!
谢谢!
回答by Shiki
You're going to need to put in rule patterns in the urlManager component:
您将需要在 urlManager 组件中放入规则模式:
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');
回答by killlinuxkill
Try This......
尝试这个......
$id = Yii::app()->request->getParam('id');
回答by Manoj Kumar
in addition to @shiki's answer you can also do this
除了@shiki的回答,你也可以这样做
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;
    }

