dropelikeit / laravel-jms-serializer
Intregation JMS Serializer in Laravel / Lumen
Requires
- php: ^7.4|^8.0
- doctrine/cache: ^1.10
- illuminate/contracts: ^6.0|^8.0|^9.0
- illuminate/http: ^6.0|^8.0|^9.0
- illuminate/support: ^6.0|^8.0|^9.0
- jms/serializer: ^3.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16|^3.8
- nunomaduro/larastan: ^0.6.4
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0
- phpstan/phpstan-phpunit: ^0.12.16
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2022-05-20 23:04:30 UTC
README
JMS Serializer for Laravel
This package integrates the JMS serializer into Laravel.
JMS-Serializer: https://github.com/schmittjoh/serializer
You are also welcome to use the Issue Tracker to set bugs, improvements or upgrade requests.
Installation
composer require dropelikeit/laravel-jms-serializer
Support note
Laravel 5.* is no longer supported with release v2.0.0 and higher.
How to use
Laravel uses Package Auto-Discovery, so you do not need to add the service provider manually.
For example, to use the JMS serializer in a controller, add the ResponseFactory in the constructor.
<?php namespace App\Http\Controller; use Dropelikeit\LaravelJmsSerializer\ResponseFactory; use Symfony\Component\HttpFoundation\JsonResponse; class ExampleController extends Controller { /** * @var ResponseFactory */ private $responseFactory; public function __construct(ResponseFactory $responseFactory) { $this->responseFactory = $responseFactory; } public function myAction(): JsonResponse { $myDataObjectWithSerializerAnnotations = new Object('some data'); return $this->responseFactory->create($myDataObjectWithSerializerAnnotations); } }
Publish the Serializer Config with the command
php artisan vendor:publish
After that you will see a config file in your config folder named "laravel-jms-serializer.php" with the following content:
<?php return [ 'serialize_null' => true, 'serialize_type' => Config\Config::SERIALIZE_TYPE_JSON, 'debug' => false, ];
As you can see zero values are serialized by default.
Using Custom-Context
To use your own JMS contexts, use the "withContext" method
To learn more about JMS context, read the JMS Serializer documentation: http://jmsyst.com/libs/serializer/master
<?php namespace App\Http\Controller; use Dropelikeit\LaravelJmsSerializer\ResponseFactory; use Symfony\Component\HttpFoundation\JsonResponse; use JMS\Serializer\SerializationContext; class ExampleController extends Controller { /** * @var ResponseFactory */ private $responseFactory; public function __construct(ResponseFactory $responseFactory) { $this->responseFactory = $responseFactory; } public function myAction(): JsonResponse { $myDataObjectWithSerializerAnnotations = new Object('some data'); $context = SerializationContext::create()->setSerializeNull(true); $this->responseFactory->withContext($context); return $this->responseFactory->create($myDataObjectWithSerializerAnnotations); } }
Using Status-Code
You do not always want to hand over a status code of 200 to the frontend. You can achieve this with the following code. Use the method "withStatusCode" for this
<?php namespace App\Http\Controller; use Dropelikeit\LaravelJmsSerializer\ResponseFactory; use Symfony\Component\HttpFoundation\JsonResponse; class ExampleController extends Controller { /** * @var ResponseFactory */ private $responseFactory; public function __construct(ResponseFactory $responseFactory) { $this->responseFactory = $responseFactory; } public function myAction(): JsonResponse { $myDataObjectWithSerializerAnnotations = new Object('some data'); $this->responseFactory->withStatusCode(400); return $this->responseFactory->create($myDataObjectWithSerializerAnnotations); } }