PHP code example of mhndev / hal
1. Go to this page and download the library: Download mhndev/hal 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/ */
mhndev / hal example snippets
$post = [
'title' => 'sample post title',
'text' => 'post body goes here ...',
];
$user = [
'username' => 'mhndev',
'mobile' => '09124917706',
'email' => '[email protected] '
];
$comments = [
[
'text' => 'Hi',
'uid' => 12
],
[
'text' => 'OK',
'uid' => 14
],
[
'text' => 'NOK',
'uid' => 10
]
];
$tags = [
'tag1', 'tag2', 'tag3'
];
$profile = [
'avatar' => 'http://google.com/inja.jpeg',
'name' => 'majid',
'username' => 'mhndev',
'bio' => 'user biography goes here ...'
];
$self_link = new \mhndev\hal\Link('self', 'http://google.com');
$next_link = new \mhndev\hal\Link('next', 'http://google.com');
$postResource = new \mhndev\hal\Resource($post);
$profileResource = new \mhndev\hal\Resource($profile);
$profileResource->addLink($self_link);
$userResource = new \mhndev\hal\Resource($user);
$userResource->addEmbeddedResource($profileResource, 'profile');
$tagsResource = new \mhndev\hal\Resource($tags);
$request = \Slim\Http\Request::createFromGlobals([]);
$commentsResource = new \mhndev\hal\Paginated($comments, 5 , 10, $request);
$postResource->addEmbeddedResource($userResource, 'user');
$postResource->addEmbeddedResource($tagsResource, 'tags');
$postResource->addEmbeddedResource($commentsResource, 'comments');
$postResource->addLink($next_link);
$postResource->addLink($self_link);
header('Content-Type: application/json');
$presenter = new \mhndev\hal\Presenter($postResource);
var_dump((new \mhndev\hal\Presenter($postResource))->asArray());die();
echo $presenter->asJson();