php - Yii2 REST API 作为模块路由配置

标签 php rest api yii2

我有一个现有的 Yii2 应用程序,并且一直在尝试将 REST API 实现为附加模块(也许模块不是解决此问题的正确方法?)但是我在配置路由结构时遇到了一些问题。根据following guide,它不太有效,也没有遵循预期的结果。 .

我构建了一个额外的模块,如下所示:

module
  api
    controllers
      UserController.php
    Module.php

UserController.php

<?php

namespace app\modules\api\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
}

Module.php

<?php

namespace app\modules\api;

/**
 * onco module definition class
 */
class Module extends \yii\base\Module
{
    public $defaultController = 'user';
    /**
     * @inheritdoc
     */
    public $controllerNamespace = 'app\modules\api\controllers';

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();

        // custom initialization code goes here
    }
}

在我的配置文件中添加了以下内容:

'request' => [
    ...
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]
        ],
    ...
    'urlManager' => [
      'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => false, // have tried as true also
    'rules' => [
     ...
     ['class' => 'yii\rest\UrlRule', 'controller' => '\app\modules\api\controllers\user'],
            ],
     ],
     ...
     'modules' => [
      ...
      'api' => [ // module for RESTful API
            'class' => 'app\modules\api\Module',
        ],
    ]

当我通过 postman 运行以下网址时,我得到以下信息:

我不确定为什么在文档中将预测路线标注为:

Trying it Out With the above minimal amount of effort, you have already finished your task of creating the RESTful APIs for accessing the user data. The APIs you have created include: GET /users: list all users page by page;

HEAD /users: show the overview information of user listing;

POST /users: create a new user;

GET /users/123: return the details of the user 123;

HEAD /users/123: show the overview information of user 123;

PATCH /users/123 and PUT /users/123: update the user 123;

DELETE /users/123: delete the user 123;

OPTIONS /users: show the supported verbs regarding endpoint /users;

OPTIONS /users/123: show the supported verbs regarding endpoint /users/123

在此设置中我可能做错了什么?有没有更好的方法在现有网站中实现 API,同时保持 DRY 做法?

最佳答案

试试这个:

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
         [
            'class' => 'yii\rest\UrlRule', 
            'controller' => ['api/user'],
         ]
    ]
],
...
'modules' => [
  ...
  'api' => [
        'basePath' => '@app/modules/api',
        'class' => 'app\modules\api\Module',
    ],
]

还要确保实现 prettyUrl 的 related server server configs .

关于php - Yii2 REST API 作为模块路由配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42230528/

相关文章:

sql - 如何清理 Rails API 参数

PHP警告: cannot modify header information

php - 如何将数组元素扩展为函数的单独参数

父子关系中的 Php 模块化与快速脚本执行

php - 如何使用我的 PHP 脚本中的数据动态更新 Bootstrap 进度条?

c++ - 使用 C++ 的 Web 服务

java - REST API Azure 事件中心 ExpiredToken

android - 如何使用请求体(application/json)和请求头进行API调用?

rest - Magento REST API 错误 500

r - 使用R构建RESTful API