offspring/laravel-handle-responder
Composer 安装命令:
composer require offspring/laravel-handle-responder
包简介
A Laravel package for building API responses
关键字:
README 文档
README
Laravel Responder is a package for building API responses,
Installation
To get started, install the package through Composer:
composer require offspring/laravel-handle-responder
Publish Package Assets (optional)
You may additionally publish the package configuration and language file using the vendor:publish Artisan command:
php artisan vendor:publish --provider="Offspring\Responder\ResponderServiceProvider"
This will publish a responder.php configuration file in your config folder. It will also publish an errors.php file inside your lang/en folder which can be used for storing error messages.
#### Use `responder` Helper
If you're a fan of Laravel's `response` helper function, you may like the `responder` helper function:
```php
return responder()->success();
return responder()->error();
Building Responses
The success and error methods return a SuccessResponseBuilder and ErrorResponseBuilder respectively, which both extend an abstract ResponseBuilder, giving them common behaviors. They will be converted to JSON when returned from a controller, but you can explicitly create an instance of Illuminate\Http\JsonResponse with the respond method:
return responder()->success()->respond();
return responder()->error()->respond();
The status code is set to 200 by default, but can be changed by setting the first parameter. You can also pass a list of headers as the second argument:
return responder()->success()->respond(201, ['x-foo' => true]);
return responder()->error()->respond(404, ['x-foo' => false]);
Consider always using the respond method for consistency's sake.
Casting Response Data
Instead of converting the response to a JsonResponse using the respond method, you can cast the response data to a few other types, like an array:
return responder()->success()->toArray();
return responder()->error()->toArray();
Decorating Response
A response decorator allows for last minute changes to the response before it's returned. The package comes with two response decorators out of the box adding a status and success field to the response output. The decorators key in the configuration file defines a list of all enabled response decorators:
'decorators' => [ \Offspring\Responder\Http\Responses\Decorators\StatusCodeDecorator::class, \Offspring\Responder\Http\Responses\Decorators\SuccessFlagDecorator::class, ],
You may disable a decorator by removing it from the list, or add your own decorator extending the abstract class Flugg\Responder\Http\Responses\Decorators\ResponseDecorator. You can also add additional decorators per response:
return responder()->success()->decorator(ExampleDecorator::class)->respond();
return responder()->error()->decorator(ExampleDecorator::class)->respond();
The package also ships with some situational decorators disabled by default, but which can be added to the decorator list:
PrettyPrintDecoratordecorator will beautify the JSON output;
\Offspring\Responder\Http\Responses\Decorators\PrettyPrintDecorator::class,
EscapeHtmlDecoratordecorator, based on the "sanitize input, escape output" concept, will escape HTML entities in all strings returned by your API. You can securely store input data "as is" (even malicious HTML tags) being sure that it will be outputted as un-harmful strings. Note that, using this decorator, printing data as text will result in the wrong representation and you must print it as HTML to retrieve the original value.
\Offspring\Responder\Http\Responses\Decorators\EscapeHtmlDecorator::class,
offspring/laravel-handle-responder 适用场景与选型建议
offspring/laravel-handle-responder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.15k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「responder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 offspring/laravel-handle-responder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 offspring/laravel-handle-responder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 offspring/laravel-handle-responder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
A api responder
Alfabank REST API integration
Action-Domain-Responder pattern made for Symfony.
Bloatless Endocore is a framework designed to quickly build web applications following the Action-Domain-Responder pattern.
Laravel response helpers
统计信息
- 总下载量: 9.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-04