Api: How do you set a custom serializer?

Created on 2 Jun 2015  Â·  14Comments  Â·  Source: dingo/api

I could not find a way to change the serializer. I want to use a Custom JsonApi serializer for Controller responses.

Most helpful comment

Thought I would paste this in here for anyone looking for it. You don't have to change the config.php for this to work with Dingo.

use Illuminate\Support\ServiceProvider;

class FractalServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('League\Fractal\Manager', function($app) {
            $fractal = new \League\Fractal\Manager;

            $serializer = new \League\Fractal\Serializer\JsonApiSerializer();

            $fractal->setSerializer($serializer);

            return $fractal;
        });

        $this->app->bind('Dingo\Api\Transformer\Adapter\Fractal', function($app) {
            $fractal = $app->make('\League\Fractal\Manager');

            return new \Dingo\Api\Transformer\Adapter\Fractal($fractal);
        });
    }
}

All 14 comments

You can do this within your configuration.

'transformer' => function ($app) {
    $fractal = new League\Fractal\Manager;

    $fractal->setSerializer(new YourSerializer);

    return new Dingo\Api\Transformer\Adapter\Fractal($fractal);
}

I use .env file with Lumen. Where do I put this config?

Sent from my iPhone

On 3 Jun 2015, at 02:16, Jason Lewis [email protected] wrote:

You can do this within your configuration.

'transformer' => function ($app) {
$fractal = new League\Fractal\Manager;

$fractal->setSerializer(new YourSerializer);

return new Dingo\Api\Transformer\Adapter\Fractal($fractal);

}
—
Reply to this email directly or view it on GitHub.

If you don't already have it in your root directory put a config folder in there and copy the api config file into that folder, then you just need to load the config before you register the provider using $app->configure('api');
http://lumen.laravel.com/docs/configuration#configuration-files

Tx @BradleyDeLar

For now I used like this:

in my .env file I added my own
API_TRANSFORMER=App\Api\CustomFractal

CustomFractal where extends

class CustomFractal extends Fractal
{
    public function __construct(FractalManager $fractal, $includeKey = 'include', $includeSeparator = ',', $eagerLoading = true)
    {
        $this->fractal = $fractal;
        $this->includeKey = $includeKey;
        $this->includeSeparator = $includeSeparator;
        $this->eagerLoading = $eagerLoading;
        $this->fractal->setSerializer(new CustomJsonApiSerializer());
    }

}

@catalinux Do you have a working JsonApiSerializer to share?

Fractal ships with one does it not?

On Wed, 3 Jun 2015 19:03 Vincent Klaiber [email protected] wrote:

@catalinux https://github.com/catalinux Do you have a working
JsonApiSeriaizer to share?

—
Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/444#issuecomment-108255503.

It does but sadly it is not yet compatible with the new standards.

I've not kept up with the standard. It was changing frequently when I first
implemented the serializer. Feel free to send a PR to the fractal
repository if you're able to fix it.

On Wed, 3 Jun 2015 19:13 Vincent Klaiber [email protected] wrote:

It does but sadly its not yet compatible with the new standards.

—
Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/444#issuecomment-108261187.

I think @philsturgeon wanted to wait on the standard to hit 1.0. I've opened a issue in their repo. https://github.com/thephpleague/fractal/issues/187

@vinkla : nope, I do not have. Also, I do not think I will implement in the next future a "standard jsonapi".

UPDATE - probably I would implement a custom and more simple serializer for my data

@catalinux Okay, too bad.

Thought I would paste this in here for anyone looking for it. You don't have to change the config.php for this to work with Dingo.

use Illuminate\Support\ServiceProvider;

class FractalServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('League\Fractal\Manager', function($app) {
            $fractal = new \League\Fractal\Manager;

            $serializer = new \League\Fractal\Serializer\JsonApiSerializer();

            $fractal->setSerializer($serializer);

            return $fractal;
        });

        $this->app->bind('Dingo\Api\Transformer\Adapter\Fractal', function($app) {
            $fractal = $app->make('\League\Fractal\Manager');

            return new \Dingo\Api\Transformer\Adapter\Fractal($fractal);
        });
    }
}

@rossedman Thanks for the code but there's a typo in the first call of bind() method. $this->app->bind('League\Fractal\Manager', ... should be $this->app->bind('\League\Fractal\Manager', ... - notice the '\' before 'League'.

@rossedman I try your service provider but is not working for me, I create the service provider and add it to app => providers, but not setting JsonApiSerializer by default.
I need to do something else?

Was this page helpful?
0 / 5 - 0 ratings