dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.32k stars 1.25k forks source link

How do you set a custom serializer? #444

Closed catalinux closed 9 years ago

catalinux commented 9 years ago

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

jasonlewis commented 9 years ago

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);
}
catalinux commented 9 years ago

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 notifications@github.com 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.

BradleyDeLar commented 9 years ago

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

catalinux commented 9 years ago

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());
    }

}
vinkla commented 9 years ago

@catalinux Do you have a working JsonApiSerializer to share?

jasonlewis commented 9 years ago

Fractal ships with one does it not?

On Wed, 3 Jun 2015 19:03 Vincent Klaiber notifications@github.com 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.

vinkla commented 9 years ago

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

jasonlewis commented 9 years ago

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 notifications@github.com 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.

vinkla commented 9 years ago

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

catalinux commented 9 years ago

@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

vinkla commented 9 years ago

@catalinux Okay, too bad.

rossedman commented 9 years ago

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);
        });
    }
}
ozanmuyes commented 8 years ago

@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'.

antfig commented 7 years ago

@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?