PHP code example of vivre-tech / rest-renderer
1. Go to this page and download the library: Download vivre-tech/rest-renderer 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/ */
vivre-tech / rest-renderer example snippets
use vivretech\rest\renderer\DataRenderer;
class DummyModelRenderer extends DataRenderer
{
/**
* @param array $params
* @return mixed
*/
public function renderMain($params = [])
{
return [];
}
public function renderSummary($model)
{
return [
'name' => $model['name'],
'price' => $model['price'],
];
}
public function renderDetailed($model)
{
return [
'id' => $model['id'],
'name' => $model['name'],
'price' => $model['price'],
'created_at' => $model['created_at'],
];
}
}
$render = new DummyModelRenderer();
$productModel = [
'id' => 1,
'name' => 'Product 1',
'price' => 100,
'created_at' => date('Y-m-d H:i:s')
];
/* Output JSON -> Dummy REST response. */
header("Content-Type: application/json;charset=utf-8");
echo
json_encode([
'productSummary' => $render->run('summary', [$productModel]),
'productDetailed' => $render->run('Detailed', [$productModel]),
]);