apsxj/json
Composer 安装命令:
composer require apsxj/json
包简介
JSON 1.0 Library for Web APIs
README 文档
README
JSON 1.0 Library for Web APIs
This library was designed using the JSON:API v1.0 STABLE specification.
Getting Started
- Add the following to your
composer.jsonfile:
"require": { "apsxj/json": "dev-main" }, "repositories": [ { "type": "git", "url": "https://github.com/apsxj/json.git" } ]
-
Run
composer install -
Before calling any of the methods, require the vendor autoloader
// For example, from the root directory... require_once(__DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
- To create a
Responseobject and render it:
<?php // unwrap the Response class from the apsxj\json namespace use apsxj\json\Response; // Create a new response $response = new Response(); // Add some data $response->appendData( array( 'method' => 'test', 'result' => 'success' ) ); // Render the response $response->render();
Output should look something like this:
{ "links": { "self": "https:\/\/localhost\/api\/test" }, "meta": { "timestamp": 1620587659, "object": "dictionary", "count": 1 }, "data": [ { "method": "test", "result": "success" } ] }
- To create an error response and render it:
<?php // unwrap the Response class from the apsxj\json namespace use apsxj\json\Response; // Create a new response $response = new Response(); // Add the error $response->appendError( 404, 'Not Found', 'The requested resource was not found' ); // Render the response $response->render();
Output should look something like this:
{ "errors": [ { "status": 404, "title": "Not Found", "detail": "The requested resource was not found" } ] }
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-09