adt/api-json-router
Composer 安装命令:
composer require adt/api-json-router
包简介
RESTful Router for Nette with Json schema specification and verification
README 文档
README
Creates a route that expects Json in request body, checks this body against Json schema supplied to the route and passes top level parameters to actions.
ApiJsonRouter extends Contributte api-router thus see the documentation for full routes definition.
Opis JSON Schema is used for full Json Schema standard validation.
Standard "draft-2020-12" is used for validation as default.
Installation
composer require adt/api-json-router
Example
Router
final class RouterFactory { // Prepare your route specification public static function getApiRouteSpecification(): array { return [ '/api/devices>POST' => [ 'path' => '/api/devices/<uuid>/request', 'presenter' => 'DeviceRequest', 'method' => 'POST', 'action' => 'create', 'parameters' => [ 'uuid' => ['type' => 'string', 'requirement' => '.+'], ], 'body' => [ 'type' => 'object', 'properties' => [ 'type' => ['type' => 'string'], ], 'required' => ['type'] ], 'title' => 'Create a request', 'description' => 'Create a request for a specific device.', ] ]; } // Create API router public static function createRouter(): \ADT\ApiJsonRouter\ApiRouteList { $apiRouter = new \ADT\ApiJsonRouter\ApiRouteList('Api'); $apiRouter->addRoutesBySpecification(self::getApiRouteSpecification()); return $apiRouter; } }
DeviceRequest presenter
class DeviceRequestPresenter extends Presenter { public function actionCreate(string $uuid, string $_type) { // Check if you have access to a specific device if ($this->deviceQuery->create()->byUuid($uuid)) { $this->sendJsonError(404); } // Create a device request $deviceRequest = new DeviceRequest($device, $_type) $this->em->persist(); $this->em->flush(); // Send a response $this->sendJson(['uuid' => $deviceRequest->getUuid(), 'type' => $device->getType()]); } }
Docs presenter
class DocsPresenter extends Presenter { public function actionDefault() { // Generate API documentation in Markdown format $apiDocumentation = new ADT\ApiJsonRouter\ApiDocumentation('API Docs', RouterFactory::getApiRouteSpecification()); $markdownApiDocumentation = $apiDocumentation->getDocumentation(); // Generate API documentation in HTML format // e.g. you can use https://github.com/erusev/parsedown die((new \Parsedown())->text($markdownApiDocumentation)); } }
Error presenter
class ErrorPresenter extends Presenter { public function renderDefault(Exception $exception) { if ($exception instanceof ApiException) { $this->sendJsonError($exception->getCode(), $exception->getMessage()); } Debugger::log($exception, ILogger::EXCEPTION); $this->sendJsonError(500, 'There was an error processing your request. Please try again later.'); } }
adt/api-json-router 适用场景与选型建议
adt/api-json-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.8k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 adt/api-json-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adt/api-json-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-21