|
1 | 1 | # Symfony JSON rest API [](https://travis-ci.org/rezzza/symfony-json-rest-api)
|
2 | 2 |
|
3 |
| -Symfony stuff to help building a json rest api |
| 3 | +Symfony stuff to help building a JSON REST api : |
| 4 | +- Support JSON payload |
| 5 | +- Support LINK headers |
| 6 | +- Handle exception as JSON response |
| 7 | +- Possibility to validate json payload through json schema |
| 8 | +- Possibility to map custom exception to http code |
4 | 9 |
|
5 |
| -We will find : |
6 |
| -* [JsonBodyListener](JsonBodyListener.php) to convert raw JSON from body of your requets into request attributes |
7 |
| -* [LinkRequestListener](LinkRequestListener.php) to validate and transform link header into request attributes |
8 |
| -* [JsonExceptionHandler](JsonExceptionHandler.php) to transform exception into JsonResponse |
| 10 | +Here is a simple setup to start : |
| 11 | +```xml |
| 12 | +<?xml version="1.0" ?> |
9 | 13 |
|
10 |
| -See usage into our [symfony-micro-edition](https://github.com/rezzza/symfony-micro-edition/blob/master/app/services/ui.xml) |
| 14 | +<container xmlns="http://symfony.com/schema/dic/services" |
| 15 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 16 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 17 | + |
| 18 | + <services> |
| 19 | + <service id="json_body_listener.ui" class="Rezzza\SymfonyRestApiJson\JsonBodyListener"> |
| 20 | + <argument type="service"> |
| 21 | + <service class="Rezzza\SymfonyRestApiJson\PayloadValidator"> |
| 22 | + <argument type="service"> |
| 23 | + <service class="Rezzza\SymfonyRestApiJson\JsonSchemaTools" /> |
| 24 | + </argument> |
| 25 | + </service> |
| 26 | + </argument> |
| 27 | + <tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="24" /> |
| 28 | + </service> |
| 29 | + |
| 30 | + <service id="link_request_listener.ui" class="Rezzza\SymfonyRestApiJson\LinkRequestListener"> |
| 31 | + <tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="10" /> |
| 32 | + </service> |
| 33 | + |
| 34 | + <service id="json_exception_handler.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionHandler"> |
| 35 | + <argument type="service"> |
| 36 | + <service class="Rezzza\SymfonyRestApiJson\ExceptionHttpCodeMap"> |
| 37 | + <argument>%exception_http_code_map%</argument> |
| 38 | + </service> |
| 39 | + </argument> |
| 40 | + <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="32" /> |
| 41 | + </service> |
| 42 | + |
| 43 | + <service id="json_exception_controller.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionController"> |
| 44 | + <argument>%kernel.debug%</argument> |
| 45 | + <argument>%show_exception_token%</argument> |
| 46 | + </service> |
| 47 | + |
| 48 | + <service id="ui.event_listener.exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener"> |
| 49 | + <tag name="kernel.event_subscriber" /> |
| 50 | + <tag name="monolog.logger" channel="request" /> |
| 51 | + <argument>json_exception_controller.ui:showException</argument> |
| 52 | + <argument type="service" id="logger" on-invalid="null" /> |
| 53 | + </service> |
| 54 | + </services> |
| 55 | +</container> |
| 56 | +``` |
0 commit comments