smhnaqvi/json-response 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

smhnaqvi/json-response

Composer 安装命令:

composer require smhnaqvi/json-response

包简介

simple library for rest apis to create easily json response

README 文档

README

simple library for rest apis to create easily json response

// create response instance
$res = new Response();

::setStatus

set your response status accept boolean value true or false

$res->setStatus(false); // default value is false 

::setData

you can set data using this method accept any format data such as string,object,array,boolean,integer.

$data = array(
    "first_name" => "syed mohammad hussain",
    "last_name" => "naqvi",
);
$res->setData($data);

::setErrors

you can set your errors for response using this method accept Error instance or null data format.

//pass array of errors codes
$res->setErrors([1100, 1101, 1102, 1105]);

// pass errors code with text message
$res->setErrors([
    array("code" => 1100, "message" => "user not found!")
]);

// or you can use Error instance to manage better and more comfortable your errors
$errors = new Errors();
$errors->addErrorCode(1100);
$errors->addErrorCode(1101);
$errors->addErrorCode(1102);
$errors->addErrorCode(1105);

$res->setErrors($errors->getErrorsCodes());

::getData

return your setup response data.

$data = $res->getData();

::getStatus

return response status boolean value.

$status = $res->getStatus();

::getErrors

return your setup error from response instance.

$errors = $res->getErrors();

::toJson

in this method set header content-type to application/json for show json response

header('Content-Type: application/json; charset=utf-8');

using exit() to stop php process and json_encode() to encode data to json .

$res->toJson();

::toArray

return array of response object has success|errors|data arrays key

$response = $res->toArray();

$success =  $response["success"];
$data =  $response["data"];
$errors =  $response["errors"];

created by @smhnaqvi

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固