2017 © Pedro Peláez
 

library serializable-api-response

Allows to return raw object as controller response so that it will be serialized later by serializer and converted to Symfony Response.

image

alcalyn/serializable-api-response

Allows to return raw object as controller response so that it will be serialized later by serializer and converted to Symfony Response.

  • Wednesday, April 25, 2018
  • by alcalyn
  • Repository
  • 0 Watchers
  • 0 Stars
  • 322 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

Serializable Api Response

If you use Symfony Http foundation and JMS Serializer, this library allows to return raw objects and status code as a controller response so that it will be serialized later by serializer and converted to a Symfony Response., (*1)

The problem: Symfony converts response content to string if we use Symfony Response, or if we return a raw object and use a filter listener, then we can't pass status code., (*2)

Installation

Install via composer, (*3)

``` js { "require": { "alcalyn/serializable-api-response": "~1.0" } }, (*4)



## Usage Return a non-yet-serialized response in your controller: ``` php use Symfony\Component\HttpKernel\Exception\ConflictHttpException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Alcalyn\SerializableApiResponse\ApiResponse; use Acme\UserApi\Exception\UserAlreadyExistsException; class UserController { /** * @param Request $request * * @return ApiResponse * * @throws ConflictHttpException if username already exists. */ public function registerUserAction(Request $request) { $username = $request->request->get('username'); $password = $request->request->get('password'); try { $user = $this->userManager->createUser($username, $password); } catch (UserAlreadyExistsException $e) { throw new ConflictHttpException('An user with username "'.$username.'" already exists.', $e); } return new ApiResponse($user, Response::HTTP_CREATED); } }

Register ApiResponse Filter Listener:, (*5)

In Silex:, (*6)

``` php use Symfony\Component\HttpKernel\KernelEvents; use Alcalyn\SerializableApiResponse\ApiResponseFilter;, (*7)

// Register reponse filter as a service $this['acme.listener.api_response_filter'] = function () { $serializer = $this['serializer']; // Assuming your serializer service has this name, (*8)

return new ApiResponseFilter($serializer);

};, (*9)

// Listen Kernel response to convert ApiResponse with raw object to Symfony Response with serialized data $this->on(KernelEvents::VIEW, function ($event) { $this['acme.listener.api_response_filter']->onKernelView($event); }); ```, (*10)

License

This project is under MIT Lisense, (*11)

The Versions

25/04 2018

dev-master

9999999-dev https://github.com/alcalyn/serializable-api-response

Allows to return raw object as controller response so that it will be serialized later by serializer and converted to Symfony Response.

  Sources   Download

MIT

The Requires

 

serializer symfony

25/04 2018

1.1.0

1.1.0.0 https://github.com/alcalyn/serializable-api-response

Allows to return raw object as controller response so that it will be serialized later by serializer and converted to Symfony Response.

  Sources   Download

MIT

The Requires

 

serializer symfony

30/05 2016

1.0.0

1.0.0.0 https://github.com/alcalyn/serializable-api-response

Allows to return raw object as controller response so that it will be serialized later by serializer and converted to Symfony Response.

  Sources   Download

MIT

The Requires

 

serializer symfony