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

标签 php yii2

我正在尝试使用默认 Controller 在 Yii2 中使用 RESTfull web 服务。但我面临的问题是,我无法发送带参数的 POST 请求。 下面是我的代码:

web.php 中的 Url 管理器规则

'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 文件在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);
    }

}

但是当我向 http://{url}//api/v1/auth/payment/5 发送 GET 请求时,我得到的响应是 5 。但我想在发送 POST 或任何其他方法时获得该结果。

那么我该如何实现呢?

最佳答案

让我告诉你我是如何解决它的for my application .

我为基本应用程序设置构建了一个简单的应用程序结构。

------ 应用

------模块

------------API

------------模块

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

-------------------- Controller

----------------模型

--------------------等等

您可以先简单地完成应用程序设置。

然后在每个 Controller 中定义动词过滤或通过在公共(public)类中定义将其分类,就像我所做的那样here.

然后使用您在 urlManager 中定义的相同规则,您将能够获取请求查询参数。

希望这对您有所帮助。

关于php - 如何将 Yii2 自定义 URL 规则中的请求方法从 GET 更改为 POST 等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28529365/

相关文章:

php - 复选框和 Javascript、PHP 的问题

javascript - Google 图表中的报价错误 "Data column(s) for axis #0 cannot be of type string"

php - Codeigniter 的 `where` 和 `or_where`

elasticsearch - Elasticsearch构面过滤器

php - Yii2 - 使用 DateValidator 验证 ISO 8601 日期时间格式

php - 将 MySQL 查询转换为 Codeigniter 语法

php - 我们何时/何地使用 PDO?

php - 根据模式生成唯一的 id

yii2 - 如何在Yii2中通过runAction传递操作中的参数

php - yii2 ActiveRecord 通过计算查找 OrderBy