Yii 框架:控制器/动作 url &参数

2022-01-04 00:00:00 path url php yii

在我的应用程序中,我有 ApiControlleractionUsers,所以在 YII 中,路径变为 api/users.现在为了获取某些用户信息,我使用以下路径 api/users/id/10 其中 10 是用户 ID,路径的 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:

Yii 框架文档:url

您的配置应如下所示:

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

相关文章