Routing ======= The RestBundle provides custom route loaders to help in defining REST friendly routes as well as reducing the manual work of configuring routes and the given requirements (like making sure that only GET may be used in certain routes etc.). You may specify a ``default_format`` that the routing loader will use for the ``_format`` parameter if none is specified. ```yaml # app/config/config.yml fos_rest: routing_loader: default_format: json ``` Many of the features explained below are used in the following example code: https://github.com/liip/LiipHelloBundle/blob/master/Controller/RestController.php Single RESTful controller routes ================================ In this section we are looking at controllers for resources without sub-resources. Handling of sub-resources requires some additional considerations which are explained in the next section. ```yaml # app/config/routing.yml users: type: rest resource: Acme\HelloBundle\Controller\UsersController ``` This will tell Symfony2 to automatically generate proper REST routes from your ``UsersController`` action names. Notice ``type: rest`` option. It's required so that the RestBundle can find which routes are supported. Notice the ``name_prefix: my_bundle_`` option. It's useful to prefix the generated controller routes to organize your several resources paths. Take care that you can use ``name_prefix`` on an import only when the file is imported itself with the type ``rest``. The parent option is used in sub-resources as we will see in the next section for multiple RESTful controller routes. ## Define resource actions ```php ; rel="kind_of_relation"`` * **unlink** - this action accepts *UNLINK* requests to the url */resources/{id}* and is supposed to return nothing but a status code indicating that the specified resources were unlinked. It is used to declare that some resources are not related anymore. When calling a UNLINK url you must provide in your header at least one link header formatted as follow : ``; rel="kind_of_relation"`` Important note about **link** and **unlink**: The implementation of the request listener extracting the resources as entities is not provided by this bundle. A good implementation can be found here : http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/ It also contains some examples on how to use it. **link** and **unlink** were obsoleted by RFC 2616, RFC 5988 aims to define it in a more clear way. Using these methods is not risky, but remains unclear (cf. issues 323 and 325). ### Conventional Actions HATEOAS, or Hypermedia as the Engine of Application State, is an aspect of REST which allows clients to interact with the REST service with hypertext - most commonly through an HTML page. There are 3 Conventional Action routings that are supported by this bundle: * **new** - A hypermedia representation that acts as the engine to *POST*. Typically this is a form that allows the client to *POST* a new resource. Shown as ``UsersController::newUsersAction()`` above. * **edit** - A hypermedia representation that acts as the engine to *PUT*. Typically this is a form that allows the client to *PUT*, or update, an existing resource. Shown as ``UsersController::editUserAction()`` above. * **remove** - A hypermedia representation that acts as the engine to *DELETE*. Typically this is a form that allows the client to *DELETE* an existing resource. Commonly a confirmation form. Shown as ``UsersController::removeUserAction()`` above. ### Custom PATCH Actions All actions that do not match the ones listed in the sections above will register as a *PATCH* action. In the controller shown above, these actions are ``UsersController::lockUserAction()``, ``UsersController::banUserAction()`` and ``UsersController::voteUserCommentAction()``. You could just as easily create a method called ``UsersController::promoteUserAction()`` which would take a *PATCH* request to the url */users/{slug}/promote*. This allows for easy updating of aspects of a resource, without having to deal with the resource as a whole at the standard *PATCH* or *PUT* endpoint. ### Sub-Resource Actions Of course it's possible and common to have sub or child resources. They are easily defined within the same controller by following the naming convention ``ResourceController::actionResourceSubResource()`` - as seen in the example above with ``UsersController::getUserCommentsAction()``. This is a good strategy to follow when the child resource needs the parent resource's ID in order to look up itself. ### Optional {_format} in route By default, routes are generated with {_format} string. If you want to get clean urls (``/orders`` instead ``/orders.{_format}``) then all you have to do is add some configuration: ```yml # app/config/config.yml fos_rest: routing_loader: include_format: false ``` ## That was it! [Return to the index](index.md) or continue reading about [Automatic route generation: multiple RESTful controllers](6-automatic-route-generation_multiple-restful-controllers.md).