定制 rohan0793/jsend 二次开发

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

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

rohan0793/jsend

最新稳定版本:1.2.3

Composer 安装命令:

composer require rohan0793/jsend

包简介

To define a JSend response macro for Laravel 5 applications

README 文档

README

Build Status Scrutinizer Code Quality Build Status

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.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-02-12