定制 shalvah/laravel-jsend 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

shalvah/laravel-jsend

Composer 安装命令:

composer require shalvah/laravel-jsend

包简介

Laravel helpers to easily send JSend-compliant responses

README 文档

README

Latest Stable Version Total Downloads

Simple helpers to generate JSend-compliant responses for your Laravel app

The JSend specification lays down some rules for how JSON responses from web servers should be formatted. JSend is especially suited for REST-style applications and APIs.

Installation

Laravel 7 and above:

composer require shalvah/laravel-jsend

Laravel 5.1 - 6.*:

composer require shalvah/laravel-jsend:^1.0

Usage

In your controller:

public function create(Request $request)
{
  $userData = $request->input('data');
  if (empty($userData['email']))
      return jsend_fail(['email' => 'Email is required']);
  
  try {
      $user = User::create($userData):
      return jsend_success($user);
  } catch (Exception $e) {
      return jsend_error('Unable to create user: '.$e->getMessage());
  }
}

You can also add the JsendExceptionFormatter trait in App\Exceptions\Handler to format JSON responses for unhandled exceptions and Laravel validation errors as JSend:

class Handler extends ExceptionHandler
{
    use Shalvah\LaravelJsend\JsendExceptionFormatter;
    
    // ...
}

Available helpers

jsend_success

The jsend_success function creates a JSend success response instance.

return jsend_success([
  "id" => 2,
  "title" => "New life",
  "body" => "Trust me, this is great!"
]);

Generates a response:

{
  "status": "success",
  "data": {
    "id": 2,
    "title": "New life",
    "body": "Trust me, this is great!"
  }
}

You may pass an Eloquent model instead of an array as the "data" object:

$post = Post::find($id);
return jsend_success($post);

jsend_fail

The jsend_fail function creates a JSend fail response instance.

return jsend_fail([
    "title" => "title is required",
    "body" => "body must be 50 - 10000 words"
]);

Generates a response:

{
  "status": "fail",
  "data": {
    "title": "title is required",
    "body": "body must be 50 - 10000 words"
  }
}

jsend_error

The jsend_error function creates a JSend error response instance.

return jsend_error("Unable to connect to database");

Generates a response:

{
  "status": "error",
  "message":"Unable to connect to database"
}

You may also pass optional code and data objects.

return jsend_error("Unable to connect to database", 'E0001', ['type' => 'database error']);

Generates a response:

{
  "status": "error",
  "message":"Unable to connect to database",
  "code": "E001",
  "data": {
    "type": "database error"
  }
}

Note: for each helper, the HTTP status codes are set automatically (to 200, 400, and 500 for success, fail, and error respectively), and the header Content-type: application/json is set. If you wish, you may specify a HTTP status code and additional headers as the last two parameters.

return jsend_success($post, 201, ["X-My-Header" => "header value"]);
return jsend_fail(["location_id" => "Location not found"], 404);
return jsend_error("Unable to connect to database", 'E0001', [], 503, ["X-My-Header" => "header value"]);

License

MIT

shalvah/laravel-jsend 适用场景与选型建议

shalvah/laravel-jsend 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124.43k 次下载、GitHub Stars 达 62, 最近一次更新时间为 2017 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 shalvah/laravel-jsend 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 124.43k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 62
  • 点击次数: 12
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 62
  • Watchers: 3
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-21