1. Go to this page and download the library: Download gdebrauwer/laravel-hateoas 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/ */
gdebrauwer / laravel-hateoas example snippets
class MessageHateoas
{
use CreatesLinks;
public function self(Message $message) : ?Link
{
if (! auth()->user()->can('view', $message)) {
return;
}
return $this->link('message.show', ['message' => $message]);
}
public function delete(Message $message) : ?Link
{
if (! auth()->user()->can('delete', $message)) {
return $this->link('message.archive', ['message' => $message]);
}
return $this->link('message.destroy', ['message' => $message]);
}
}
class MessageResource extends JsonResource
{
use HasLinks;
public function toArray($request) : array
{
return [
'id' => $this->id,
'text' => $this->text,
'_links' => $this->links(),
];
}
}
use GDebrauwer\Hateoas\Hateoas;
use GDebrauwer\Hateoas\LinkCollection;
// Provide your own Formatter class ...
Hateoas::formatLinksUsing(CustomFormatter::class);
// ... Or provide a callback
Hateoas::formatLinksUsing(function (LinkCollection $links) {
// return array based on links
});
use GDebrauwer\Hateoas\Hateoas;
Hateoas::guessHateoasClassNameUsing(function (string $class) {
// return a HATEOAS class name
});