PHPackages                             nilportugues/laravel5-haljson - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. nilportugues/laravel5-haljson

ActiveLibrary[API Development](/categories/api)

nilportugues/laravel5-haljson
=============================

Laravel 5 HAL+JSON API Transformer Package

1.4.0(9y ago)151.0k8MITPHP

Since Aug 16Pushed 9y agoCompare

[ Source](https://github.com/nilportugues/laravel5-hal-json-transformer)[ Packagist](https://packagist.org/packages/nilportugues/laravel5-haljson)[ Docs](http://nilportugues.com)[ RSS](/packages/nilportugues-laravel5-haljson/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (22)Used By (0)

Laravel 5 HAL+JSON
==================

[](#laravel-5-haljson)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9a6faabdcc3b819e7cc660cb84a8a9ac5e1507e78d1a618eb980702dd7c47fb4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e696c706f727475677565732f6c61726176656c352d68616c2d6a736f6e2d7472616e73666f726d65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nilportugues/laravel5-hal-json-transformer/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/1cbe9edffd3b290e7898a8c7cacd74e5a35df674c89bad648dbf27006dc12c5c/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f39333032396438652d373035322d343265302d613764622d6661626264326535363664352f6d696e692e706e673f)](https://insight.sensiolabs.com/projects/93029d8e-7052-42e0-a7db-fabbd2e566d5)[![Latest Stable Version](https://camo.githubusercontent.com/b0fc0fc16bb5966f3981e56164fd48db7491566ccff5427cb0db1808812b5bdd/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6c61726176656c352d68616c6a736f6e2f762f737461626c653f)](https://packagist.org/packages/nilportugues/laravel5-haljson)[![Total Downloads](https://camo.githubusercontent.com/53a8071b66d74c85b91aa55bf3916be252e2e62e48f6304914ef3465446dc815/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6c61726176656c352d68616c6a736f6e2f646f776e6c6f6164733f)](https://packagist.org/packages/nilportugues/laravel5-haljson)[![License](https://camo.githubusercontent.com/53f63844ba67f4fb82bf92faa4e5546e83ea1dde5eea7d10ded58d5cb1ae6dad/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6c61726176656c352d68616c6a736f6e2f6c6963656e73653f)](https://packagist.org/packages/nilportugues/laravel5-haljson)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://paypal.me/nilportugues)

1. [Installation](#1-installation)
2. [Configuration](#2-configuration)
3. [Mapping](#3-mapping)
    - 3.1 [Mapping with arrays](#31-mapping-with-arrays)
    - 3.2 [Mapping with Mapping class](#32-mapping-with-mapping-class)
4. [HAL Serialization](#4-hal-serialization)
5. [HAL Paginated Resource](#5-hal-paginated-resource)
6. [PSR-7 Response Objects](#6-response-objects)

1. Installation
---------------

[](#1-installation)

Use [Composer](https://getcomposer.org) to install the package:

```
$ composer require nilportugues/laravel5-haljson

```

2. Configuration
----------------

[](#2-configuration)

Open up `config/app.php` and add the following line under `providers` array:

```
'providers' => [

    //...
    \NilPortugues\Laravel5\HalJson\Laravel5HalJsonServiceProvider::class,
],
```

Also, enable Facades by uncommenting:

```
$app->withFacades();
```

3. Mapping
----------

[](#3-mapping)

For instance, lets say the following object has been fetched from a Repository , lets say `PostRepository` - this being implemented in Eloquent or whatever your flavour is:

```
use Acme\Domain\Dummy\Post;
use Acme\Domain\Dummy\ValueObject\PostId;
use Acme\Domain\Dummy\User;
use Acme\Domain\Dummy\ValueObject\UserId;
use Acme\Domain\Dummy\Comment;
use Acme\Domain\Dummy\ValueObject\CommentId;

//$postId = 9;
//PostRepository::findById($postId);

$post = new Post(
  new PostId(9),
  'Hello World',
  'Your first post',
  new User(
      new UserId(1),
      'Post Author'
  ),
  [
      new Comment(
          new CommentId(1000),
          'Have no fear, sers, your king is safe.',
          new User(new UserId(2), 'Barristan Selmy'),
          [
              'created_at' => (new \DateTime('2015/07/18 12:13:00'))->format('c'),
              'accepted_at' => (new \DateTime('2015/07/19 00:00:00'))->format('c'),
          ]
      ),
  ]
);
```

We will have to map all the involved classes. This can be done as one single array, or a series of Mapping classes.

Also we will require to have routes. The routes must be named routes.

For instance our `app/Http/routes.php` file contains the following routes:

```
Route::get(
  '/docs/rels/{rel}',
  ['as' => 'get_example_curie_rel', 'uses' => 'ExampleCurieController@getRelAction']
);

Route::get(
  '/post/{postId}',
  ['as' => 'get_post', 'uses' => 'PostController@getPostAction']
);

Route::get(
  '/post/{postId}/comments',
  ['as' => 'get_post_comments', 'uses' => 'CommentsController@getPostCommentsAction']
);

//...
```

### 3.1 Mapping with arrays

[](#31-mapping-with-arrays)

Create a `haljson.php` file in `config/` directory. This file should return an array returning all the class mappings.

And a series of mappings, placed in `bootstrap/haljson.php`, that require to use *named routes* so we can use the `route()` helper function:

```
