ahmedebead/cake-rest-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ahmedebead/cake-rest-api

Composer 安装命令:

composer require ahmedebead/cake-rest-api

包简介

Rest API plugin for CakePHP 4.x

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

This plugin simplifies the CakeRestApi API development for your CakePHP 4.x application. It simply converts the output of your controller into a JSON response.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require ahmedebead/cake-rest-api "^0.1"

After installation, Load the plugin

Add this file to /src/Application.php

// src/Application.php


 public function bootstrap(): void
    {
        $this->addPlugin('CakeRestApi', ['bootstrap' => true]);
        
        // Other code
    }

Or, you can load the plugin using the shell command

$ bin/cake plugin load CakeRestApi

Usage

No major change requrires in the way you code in your CakePHP application. Simply, just add one parameter to your route configuration isRest like,

$routes->connect('/foo/bar', ['controller' => 'Foo', 'action' => 'bar', 'isRest' => true]);

And extend your controller to RestController and everything will be handled by the plugin itself. For example,

<?php

namespace App\Controller;

use CakeRestApi\Controller\RestController;

/**
 * Foo Controller
 *
 */
class FooController extends CakeRestApiController
{

    /**
     * bar method
     *
     * @return Response|void
     */
    public function bar()
    {
        $bar = [
            'falanu' => [
                'dhikanu',
                'tamburo'
            ]
        ];

        $this->set(compact('bar'));
    }
}

And that's it. You will see the response as below.

{
    "status": "OK",
    "result": {
        "bar": {
            "falanu": [
                "dhikanu",
                "tamburo"
            ]
        }
    }
}

Doesn't it too simple? Whatever viewVars you set from your controller's action using set() method, will be converted into JSON response.

Response Format

This plugin returns the response in the following format.

{
    "status": "OK",
    "result": {
        ...
    }
}

The status key may contain OK or NOK based on your response code. For all successful responses, the code will be 200 and the value of this key will be OK.

In case of error or exception, the value of status will become NOK. Also, based on your application's debug setting, it will contain the exception and trace data.

The result key contains the actual response. It holds all the variables set from your controller. This key will not be available in case of error/exception.

These properties are also available in your controller's beforeFilter method, so you can put additional authentication logic there.

Reporting Issues

If you have a problem with this plugin or found any bug, please open an issue on GitHub.

ahmedebead/cake-rest-api 适用场景与选型建议

ahmedebead/cake-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 「cakephp」 「REST API」 「CakePHP CakeRestApi」 「CakePHP CakeRestApi Api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ahmedebead/cake-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ahmedebead/cake-rest-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 ahmedebead/cake-rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-08