douci4026 2015-02-15 18:12
浏览 750
已采纳

如何将Yii2自定义URL规则中的请求方法从GET更改为POST等?

I am trying to work in RESTfull web services in Yii2 using default controller. But the problem I faced is, I cannot send POST request with parameters. Below is my code:

Url Manager rule in web.php

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
            ['pattern' => 'api/v1/auth/payment/<id:\d+>', 'route' => 'api/v1/auth/payment'],
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ],

AuthController.php file this is inside controller/api/v1/

namespace app\controllers\api\v1;

use app\controllers\api\v1\components\ApiFunctions;
use Yii;
use yii\web\Controller;


class AuthController extends Controller
{
    public function actionPayment()
    {
        $id = Yii::$app->getRequest()->getQueryParam('id');
        json_encode($id);
    }

}

But when I send GET request to http://{url}//api/v1/auth/payment/5 I get response as 5. But I want to get that result when sending POST or any other methods.

So how can I achieve that?

  • 写回答

2条回答 默认 最新

  • doujiao8491 2015-02-17 13:38
    关注

    Let me show you how i solve it for my application.

    A simple application structure i have constructed for basic application setup was.

    ------ app

    ------modules

    ----------api

    -------------modules

    ----------------v1

    -------------------controllers

    -------------------models

    -------------------etc

    You can simply go through the application setup first.

    Then define verb filtering in every controller or sort it out by defining in a common class, as i have done here.

    Then with the same rule you have defined in urlManager, you will able be to get the request query parameter.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂