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

发布于 2024-08-31 20:55:30 字数 313 浏览 0 评论 0原文

在我的应用程序中,我有 ApiControlleractionUsers,因此在 YII 中路径变为 api/users。现在为了获取某些用户信息,我使用以下路径 api/users/id/10,其中 10 是用户 ID,路径的 id 部分基本上是 GET参数(api/users?id=10)。

有没有办法在路径中没有 id 部分的情况下执行相同的操作,即我希望我的路径看起来像 api/users/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).

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈独秀 2024-09-07 20:55:30

您需要在 urlManager 组件中放入规则模式:

Yii 框架文档: url

您的配置应如下所示:

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'api/users/<id>'=>'api/users',
            ),
        ),
    ),
);

然后您可以通过以下方式获取值:

$id = Yii::app()->getRequest()->getQuery('id');

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');
笙痞 2024-09-07 20:55:30

试试这个......

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

Try This......

$id = Yii::app()->request->getParam('id');
梦中的蝴蝶 2024-09-07 20:55:30

除了@shiki的回答之外,你还可以这样做

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'api/users/<id>'=>'api/users',
            ),
        ),
    ),
);

并采取行动

public function actionUsers($id=null)  // argument variable should same as in urlmanager
    {
     echo $id;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文