rompetomp / inertia-bundle

Inertia.js server-side adapter for Symfony
MIT License
152 stars 39 forks source link

Support for the Serializer component #10

Closed innocenzi closed 5 years ago

innocenzi commented 5 years ago

I think integrating the serializer component when it's available is a good idea.

I took inspiration from the json() method of Symfony's ControllerTrait and put that in the Twig\InertiaExtension class. I also added a third shared variable for giving context to the serialization.

Basically, if the serialization component is not provided, the Twig extension still uses json_encode. But when it's available, it serializes the $page with the given $context.

The context must be added as a parameter in the Twig template:

{% app.html.twig %}
{{ inertia(page, context) }}

And you can either add shared context in an event listener like this:

// ControlerSubscriber.php
$this->inertia->context('groups', [ 'id_only' ]);

Or add a specific context with the render method:

// SomeController.php
return $this->inertia->render('Dashboard', [ 'clients' => $clients ], [], [ 'groups' => 'id_only' ]);
rompetomp commented 5 years ago

Hi

Can you fix the StyleCI issues? Also remove the typos here: https://github.com/rompetomp/inertia-bundle/pull/10/files#diff-40fe6fa51ee72630ed12a2bd2a76ade9R14

Is it possible to add tests for this feature?

innocenzi commented 5 years ago

Good looking out. Fixed those. I have added simple tests for the contexts, but I'm not sure how to test the serialization.

rompetomp commented 5 years ago

Thanks for the PR @hawezo

innocenzi commented 5 years ago

@rompetomp Any way to make a release someday? Thanks a lot 👍

rompetomp commented 5 years ago

Woops, sorry. Forgot about that. I'll do that right now ;-)