PHP code example of restful-template / response-template
1. Go to this page and download the library: Download restful-template/response-template library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
restful-template / response-template example snippets
use RESTfulTemplate\ResponseTemplate;
$rest = new ResponseTemplate( 200 );
$response = $rest->build();
// $response contains following array:
// [
// "status" => [
// "code" => 200,
// "message" => "Ok."
// ],
// "data" => null,
// "links" => [
// "self" => [
// "url" => "https://example.com/",
// "method" => "GET"
// ]
// ]
// ];
use RESTfulTemplate\ResponseTemplate;
$product = [
"id" => 1,
"name" => "product-name",
"displayName" => "Product Name",
"price" => "14.50",
"stock" => 20
];
$rest = new ResponseTemplate( 200 );
$response = $rest->build( $product );
// $response contains following array:
// [
// "status" => [
// "code" => 200,
// "message" => "Ok."
// ],
// "data" => [
// "id" => 1,
// "name" => "product-name",
// "displayName" => "Product Name",
// "price" => "14.50",
// "stock" => 20
// ],
// "links" => [
// "self" => [
// "url" => "https://example.com/products/1",
// "method" => "GET"
// ]
// ]
// ];
use RESTfulTemplate\ResponseTemplate;
$products = [
[
"id" => 1,
"name" => "product-name",
"displayName" => "Product Name",
"price" => "14.50",
"stock" => 20
],
/* ...more ones */
];
$rest = new ResponseTemplate( 200 );
$rest = $rest->setLink( "next", "https://example.com/products?page=2" );
$response = $rest->build( $products );
// $response contains following array:
// [
// "status" => [
// "code" => 200,
// "message" => "Ok."
// ],
// "data" => [
// [
// "id" => 1,
// "name" => "product-name",
// "displayName" => "Product Name",
// "price" => "14.50",
// "stock" => 20
// ],
// /* ...more ones */
// ],
// "links" => [
// "self" => [
// "url" => "https://example.com/products",
// "method" => "GET"
// ],
// "next" => [
// "url" => "https://example.com/products?page=2",
// "method" => "GET"
// ]
// ]
// ];