定制 tan-sandbox/json-rest 二次开发

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

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

tan-sandbox/json-rest

Composer 安装命令:

composer require tan-sandbox/json-rest

包简介

Json response builder.

README 文档

README

Json response builder library for php.

Installation

Composer

Add to your composer.json or create a new composer.json:

{
    "require": {
        "tansandbox/json-rest": "*"
    }
}

Tell the composer to download the library by running the command:

$ php composer.phar install

To include using compser require, run the following command from your project.

$ php composer.phar require tansandbox/json-rest

Basic usages

Creating object

use TanSandbox\JsonRest\Builder;
$builder = new Builder() ;

Returning a success response

$data = array (
    'name' => 'Nithin',
    'subject' => 'English',
    'mark' => '90'
);
$builder->ok()->send($data) ;

Will produce

{
    "status": true,
    "data": {
        "name": "Nithin",
        "subject": "English",
        "mark": "90"
    }
}

Return a failure

$data = array(
    'name' => 'Please provide a valid name'
);
$builder->fail()->send($data) ;

Will produce

{
    "status": false,
    "data": {
        "name": "Please provide a valid name"
    }
}

Response with custom http status

$data = array(
    'reply' => 'Resource not found.'
);
$builder->setStatus(404)->send($data) ;

Will produce

{
    "status": false,
    "data": {
        "reply": "Resource not found."
    }
}

Advanced response

$data = array (
    'name' => 'Nithin',
    'subject' => 'English',
    'mark' => '90'
);
$builder->setMessage('Action completed')->setStatus(200)->send($data) ;

Will produce

{
    "status": false,
    "data": {
        "name": "Nithin",
        "subject": "English",
        "mark": "90"
    },
    "message": "Action completed"
}

Method chaining.

New json member can be added using the setXXX methods. The sendie() method is to output the reponse and to die after that.

$builder->setName('Nithin')
    ->setSubject('English')
    ->setMark('90')
    ->setCustomNotes('Student of ZF2')
    ->sendie() ;

Will produce

{
    "status": true,
    "message": "Action completed",
    "name": "Nithin",
    "subject": "English",
    "mark": "90",
    "customNotes": "Student of ZF2"
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-07-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固