Yii 框架:控制器/动作 url &参数 [英] Yii framework: Controller/Action url & parameters

查看:29
本文介绍了Yii 框架:控制器/动作 url &参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有 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');

这篇关于Yii 框架:控制器/动作 url &amp;参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆