Yii框架:Controller/Action url&参数 [英] Yii framework: Controller/Action url & parameters
问题描述
在我的应用程序中,我有ApiController
和actionUsers
,因此在YII中,路径变为api/users
.现在,为了获取某些用户信息,我使用以下路径api/users/id/10
,其中10是userID,路径的id
部分基本上是GET参数(api/users?id=10
).
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
).
如果没有路径的id
部分,是否有任何方法可以做相同的事情,即我希望我的路径看起来像api/users/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
?
谢谢!
推荐答案
您将需要在urlManager组件中放入规则模式:
You're going to need to put in rule patterns in the urlManager component:
您的配置应如下所示:
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');
这篇关于Yii框架:Controller/Action url&参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!