PHPackages                             thiagocordeiro/laravel-serializer - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. thiagocordeiro/laravel-serializer

ActiveProject[Parsing &amp; Serialization](/categories/parsing)

thiagocordeiro/laravel-serializer
=================================

1.0.17(1y ago)04.8k↓28.1%1MITPHPPHP &gt;=8.3

Since Mar 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/thiagocordeiro/laravel-serializer)[ Packagist](https://packagist.org/packages/thiagocordeiro/laravel-serializer)[ RSS](/packages/thiagocordeiro-laravel-serializer/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (10)Dependencies (3)Versions (20)Used By (0)

Laravel-Translator
==================

[](#laravel-translator)

Laravel-serializer serializes json body from requests into Value Objects and Value Objects into json responses. It creates factories to given objects and uses [thiagocordeiro/serializer](https://github.com/thiagocordeiro/serializer) to create encodes and decoders.

### Installation

[](#installation)

You just have to require the package

```
composer require thiagocordeiro/laravel-serializer
```

### Setting up

[](#setting-up)

This package register its provider automatically, [See laravel package discover](https://laravel.com/docs/10.x/packages#package-discovery). However if for any reason the provider doesn't get registered, then you can register the provider manually in `config/app.php` file:

```
return [
    ...
    'providers' => [
        ...
        LaravelSerializer\Framework\Providers\RequestSerializationProvider::class,
        ...
    ]
]
```

Just by adding the provider, you'll be able to configure and inject value objects into controllers, however, in orther to return these objects, it is necessary to change `Kernel.php` with a custom router configuration, so Laravel will be able to serialize configured objects as responses.

Open `App\Http\Kernel` and add a `__constructor` according to the snipped above:

```
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Routing\Router;
use LaravelSerializer\Framework\Routing\SerializerRouter;

class Kernel extends HttpKernel
{
    public function __construct(Application $app, Router $router)
    {
        parent::__construct($app, SerializerRouter::from($router));
    }

    ...
}
```

### Configure value objects

[](#configure-value-objects)

Once the provider is registered and Kernel uses SerializerRouter, you'll have to configure the objects you want to have serialization enabled.

Open or create the configuration file `config/serializer.php` and add all the objects that should be serialized according to the following snippet:

```
return [
    App\MyObject::class => [], //
];
```

### Usage

[](#usage)

Say you have an `App\Order` object and you want it to be serialized:

###### With public properties

[](#with-public-properties)

```
