Routing ======= The RestRoutingBundle 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. .. code-block:: yaml handcraftedinthealps_rest_routing: routing_loader: default_format: json formats: json: true xml: true 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. .. code-block:: yaml users: type: rest host: m.example.com resource: Acme\HelloBundle\Controller\UsersController This will tell Symfony to automatically generate proper REST routes from your ``UsersController`` action names. Notice ``type: rest`` option. It's required so that the RestRoutingBundle can find which routes are supported. Define resource actions ----------------------- .. code-block:: 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: `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: .. code-block:: yaml handcraftedinthealps_rest_routing: routing_loader: include_format: false The ``{_format}`` route requirement is automatically positioned using the available listeners. So by default, the requirement will be ``{json|xml|html}``. If you want to limit or add a custom format, you can do so by overriding it with the ``@Route`` annotation (or another one extending it, like ``@Get``, ``@Post``, ...): .. code-block:: php