rohan0793/jsend
Composer 安装命令:
composer require rohan0793/jsend
包简介
To define a JSend response macro for Laravel 5 applications
README 文档
README
jsend
This is a simple composer package to issue JSend responses from a Laravel 5 application.
To read about JSend, go through this guide
Installation
Require the package through composer:
composer require rohan0793/jsend
Add the service provider to the provider's array in your app.php config file:
RC\JSend\ResponseMacroServiceProvider::class,
Basic Example
return response()->jsend( $data = ['foo', 'bar'], $presenter = null, $status = 'success', $message = 'This is a JSend Response', $code = 200 );
Output:
{
"data": [
"foo",
"bar"
],
"status": "success",
"message": "This is a JSend Response"
}
Resource Fetched Example
return response()->resource_fetched( $data = ['foo', 'bar'] );
Output:
{
"status": "success",
"message": "Resource Fetched Successfully",
"data": [
"foo",
"bar"
]
}
The resource fetched macro is defined for ease and readability. It is calling the underlying JSend macro itself. The message, status and code will be filled automatically as success, Resource Fetched Successfully, and 200 respectively. This can useful in removing duplicated code when you have many routes for fetching different resources.
Resource Updated Example
return response()->resource_updated( $data = ['foo', 'bar'] );
{
"status": "success",
"message": "Resource Updated Successfully",
"data": [
"foo",
"bar"
]
}
Similar to resource_fetched we have resource updated. The message, status and code will be filled automatically as success, Resource Updated Successfully, and 200 respectively.
Resource Created Example
return response()->resource_updated( $data = ['foo', 'bar'] );
{
"status": "success",
"message": "Resource Created Successfully",
"data": [
"foo",
"bar"
]
}
Similar to resource_fetched we have resource_created. The message, status and code will be filled automatically as success, Resource Created Successfully, and 201 respectively.
Resource Deleted Example
return response()->resource_deleted( $data = ['foo', 'bar'] );
The resource_deleted is a little special. It will output code 204 No Content. According to HTTP code specifications, it will have no output at all.
JSend Error Example
return response()->jsend_error( new \Exception('Some Exception'), $message = null, $code = null );
{
"status": "error",
"message": "Some Exception",
"data": null
}
Finally, we have the jsend_error macro which is basically used to respond when there has been an error. It takes an exception as the first argument. It will output code and message from the exception itself. If custom message and code is passed, they will be output instead.
Presenter
Other than the basic response macros with messages and codes, this package also supports presenters. You can build your own presenters. The package will automatically call the present() method on the presenter passed. The recommended approach is using Fractal Package from The PHP League. This package provides very powerful data transforming and presenting capabilities. The documentation is extensive and easy to read and understand.
rohan0793/jsend 适用场景与选型建议
rohan0793/jsend 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.87k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「response」 「laravel」 「jsend」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rohan0793/jsend 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rohan0793/jsend 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rohan0793/jsend 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
JSendResponse Component for HttpFoundation based applications (Symfony, Silex, Drupal etc.)
Basic JSend interface for PHP projects.
JSend implementation in php
HTTP request logger middleware for Laravel
JSend port for PHP
Class to generate a standard structure for api json responses
统计信息
- 总下载量: 23.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-12