-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,56 @@ | ||
# Symfony JSON rest API [![Build Status](https://travis-ci.org/rezzza/symfony-json-rest-api.svg?branch=master)](https://travis-ci.org/rezzza/symfony-json-rest-api) | ||
|
||
Symfony stuff to help building a json rest api | ||
Symfony stuff to help building a JSON REST api : | ||
- Support JSON payload | ||
- Support LINK headers | ||
- Handle exception as JSON response | ||
- Possibility to validate json payload through json schema | ||
- Possibility to map custom exception to http code | ||
|
||
We will find : | ||
* [JsonBodyListener](JsonBodyListener.php) to convert raw JSON from body of your requets into request attributes | ||
* [LinkRequestListener](LinkRequestListener.php) to validate and transform link header into request attributes | ||
* [JsonExceptionHandler](JsonExceptionHandler.php) to transform exception into JsonResponse | ||
Here is a simple setup to start : | ||
```xml | ||
<?xml version="1.0" ?> | ||
|
||
See usage into our [symfony-micro-edition](https://github.com/rezzza/symfony-micro-edition/blob/master/app/services/ui.xml) | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="json_body_listener.ui" class="Rezzza\SymfonyRestApiJson\JsonBodyListener"> | ||
<argument type="service"> | ||
<service class="Rezzza\SymfonyRestApiJson\PayloadValidator"> | ||
<argument type="service"> | ||
<service class="Rezzza\SymfonyRestApiJson\JsonSchemaTools" /> | ||
</argument> | ||
</service> | ||
</argument> | ||
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="24" /> | ||
</service> | ||
|
||
<service id="link_request_listener.ui" class="Rezzza\SymfonyRestApiJson\LinkRequestListener"> | ||
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="10" /> | ||
</service> | ||
|
||
<service id="json_exception_handler.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionHandler"> | ||
<argument type="service"> | ||
<service class="Rezzza\SymfonyRestApiJson\ExceptionHttpCodeMap"> | ||
<argument>%exception_http_code_map%</argument> | ||
</service> | ||
</argument> | ||
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="32" /> | ||
</service> | ||
|
||
<service id="json_exception_controller.ui" class="Rezzza\SymfonyRestApiJson\JsonExceptionController"> | ||
<argument>%kernel.debug%</argument> | ||
<argument>%show_exception_token%</argument> | ||
</service> | ||
|
||
<service id="ui.event_listener.exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener"> | ||
<tag name="kernel.event_subscriber" /> | ||
<tag name="monolog.logger" channel="request" /> | ||
<argument>json_exception_controller.ui:showException</argument> | ||
<argument type="service" id="logger" on-invalid="null" /> | ||
</service> | ||
</services> | ||
</container> | ||
``` |