duancenxie2233 2014-05-13 12:37
浏览 71
已采纳

Yii-获取由正斜杠分隔的参数。

I'm developing a REST API. I found out a few extensions but the client need is drastically different from what these extensions provide.

I need a separate action for each of the request type in each controller, so I can have the full control to myself.

I'm currently using the following URLmanager rules;

'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),

Which arranges my URL this way;

http://www.mywebsite.com/index.php/<module>/<controller>/<action>

This can easily service a request like this;

http://www.mywebsite.com/index.php/api/user/register

The problem I'm facing is how to service these type of requests.

http://www.mywebsite.com/index.php/api/user/15/profile

Is there a way I can get 15 (id/pk) and "profile" as two parameters?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongzhan7253 2014-05-13 13:12
    关注

    You have to define it in urlmanager like so for e.g.:

    'rules' => array(
        'api/user/<id:\d+>/<mode:\w+>' => 'api/user/someaction',
    ),
    

    You can then go to http://www.mywebsite.com/index.php/api/user/15/profile and the call goes to api module, user controller, action someaction and passes the $_GET variables "id" and "mode" to it.

    actionSomeaction($id, $mode)
    {
        var_dump($id, $mode);
    }
    

    EDIT: to make it dynamic use this:

    'rules' => array(
        '<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>/<mode:\w+>' => '<module>/<controller>/<action>',
    ),
    

    However I would not make everything dynamic since you usually only want to pass the params id and mode to user/someaction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog