infotechnohelp/cakephp-api-handler

CakePHP 3 插件

安装次数: 1,271

依赖: 2

建议者: 0

安全: 0

星标: 0

分支: 1

类型:cakephp-plugin

1.0.12 2019-03-08 14:49 UTC

This package is auto-updated.

Last update: 2024-09-09 03:09:32 UTC


README

设置

手动添加到 APP/config/bootstrap.php

Plugin::load('ApiHandler', ['routes' => true]);

或者使用命令

bin/cake plugin load ApiHandler -r

手动替换 APP/src/Application.php

use Cake\Error\Middleware\ErrorHandlerMiddleware;use ApiHandler\Middleware\ErrorHandlerMiddleware;

使用方法

API 层

创建 API 控制器

路径: APP/src/Controller/Api

namespace App\Controller\Api;

use ApiHandler\Traits\ApiController;

class MyController extends AppContoller
{
    use ApiController;
    
    public function get()
    {
        $this->_setResponse([1, 2, 3]);
    }
}

响应结构

{ "data": {"1", "2", "3"}, "code": 200, message": null }

异常消息有值时(异常消息),数据为 null

发送请求

前缀 api/

要发送请求到 APP/src/Controller/Api/MyController→get(),使用此路径

api/my/get