承接 rrd108/cakephp-json-api-exception 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

rrd108/cakephp-json-api-exception

Composer 安装命令:

composer require rrd108/cakephp-json-api-exception

包简介

JsonApiException plugin for CakePHP

README 文档

README

Make your CakePHP 5 JSON REST API response more descriptive on errors.

For CakePHP 4 support use the CakePHP 4 branch

Supports CakePHP 5.0–5.3+. On CakePHP 5.3+ the plugin class auto-detection uses JsonApiExceptionPlugin — no change required in your Application.php.

If you want a simple solution for token authentication for a CakePHP JSON REST API, then check this: CakePHP API Token Authenticator

Installation

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

The recommended way to install is:

composer require rrd108/cakephp-json-api-exception

Then, to load the plugin either run the following command:

bin/cake plugin load JsonApiException

or manually add the following line to your app's src/Application.php file's bootstrap() function:

$this->addPlugin('JsonApiException');

done :)

How to use

Assuming you have a CakePHP JSON REST API and you want to have more informative error messages and proper response codes.

// for example in /src/Controller/UsersController.php slightly change the baked add function
use JsonApiException\Error\Exception\JsonApiException;

public function add()
{
    $user = $this->Users->newEmptyEntity();
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->getData());
        if (!$this->Users->save($user)) {
            throw new JsonApiException($user, 'Save failed');
            // throw new JsonApiException($user, 'Save failed', 418);   // you can set the response's status code in the 3rd parameter
        }
    }
    $this->set(compact('user'));
    $this->viewBuilder()->setOption('serialize', ['user']);
}

If the save failed you will get a response like this.

{
  "message": "Save failed",
  "url": "/users.json",
  "line": 12,
  "errorCount": 1,
  "errors": {
    "password": {
      "_required": "This field is required"
    }
  }
}

You can us it with an array of entities also.

// for example in /src/Controller/UsersController.php slightly change the baked add function
use JsonApiException\Error\Exception\JsonApiException;

public function bulkAdd()
{
    $users = $this->Users->newEntities($this->request->getData());
    if (!$this->Users->saveMany($users)) {
        throw new JsonApiException($users, 'Errors in request data');
    }
    $this->set(compact('users'));
    $this->viewBuilder()->setOption('serialize', ['users']);
}

If the save failed you will get a response like this. As at this point we do not have an id for the entity, the error messages can not contain it. So an empty array will be present in the errors array if an entity does not have any errors. So the number of the entity will match the number of the error entries, entity #3's error message will be the third element of the error array.

{
  "message": "Errors in request data",
  "url": "/users.json",
  "line": 12,
  "errorCount": 2,
  "errors": [
    {
      "name": {
        "_empty": "name is required"
      }
    },
    {
      "name": {
        "_empty": "name is required"
      }
    }
  ]
}

rrd108/cakephp-json-api-exception 适用场景与选型建议

rrd108/cakephp-json-api-exception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.62k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 rrd108/cakephp-json-api-exception 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-23