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 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题