@@ -6,7 +6,7 @@ Introduction
66
77The view layer makes it possible to write ``format `` (html, json, xml, etc)
88agnostic controllers, by placing a layer between the Controller and the
9- generation of the final output via the templating or a serializer.
9+ generation of the final output via a serializer.
1010
1111The bundle works both with the `Symfony Serializer Component `_ and the more
1212sophisticated `serializer `_ created by Johannes Schmitt and integrated via the
@@ -134,17 +134,11 @@ combination with SensioFrameworkExtraBundle you can even omit the calls to
134134
135135As the purpose is to create a format-agnostic controller, data assigned to the
136136``View `` instance should ideally be an object graph, though any data type is
137- acceptable. Note that when rendering templating formats, the ``ViewHandler ``
138- will wrap data types other than associative arrays in an associative array with
139- a single key (default ``'data' ``), which will become the variable name of the
140- object in the respective template. You can change this variable by calling
141- the ``setTemplateVar() `` method on the view object.
137+ acceptable.
142138
143139There are also two specialized methods for redirect in the ``View `` classes.
144140``View::createRedirect `` redirects to an URL called ``RedirectView `` and
145- ``View::createRouteRedirect `` redirects to a route. Note that whether these
146- classes actually cause a redirect or not is determined by the ``force_redirects ``
147- configuration option, which is only enabled for ``html `` by default (see below).
141+ ``View::createRouteRedirect `` redirects to a route.
148142
149143There are several more methods on the ``View `` class, here is a list of all
150144the important ones for configuring the view:
@@ -288,12 +282,9 @@ the task without any client modifications.
288282Configuration
289283-------------
290284
291- The ``formats `` and ``templating_formats `` settings determine which formats are
292- respectively supported by the serializer and by the template layer. In other
293- words any format listed in ``templating_formats `` will require a template for
294- rendering using the ``templating `` service, while any format listed in
295- ``formats `` will use the serializer for rendering. For both settings a
296- value of ``false `` means that the given format is disabled.
285+ The ``formats `` setting determines which formats are supported by the serializer.
286+ Any format listed in ``formats `` will use the serializer for rendering. A value
287+ of ``false `` means that the given format is disabled.
297288
298289When using ``RouteRedirectView::create() `` the default behavior of forcing a
299290redirect to the route when HTML is enabled, but this needs to be enabled for other
@@ -303,99 +294,6 @@ Finally the HTTP response status code for failed validation defaults to
303294``400 ``. Note when changing the default you can use name constants of
304295``Symfony\Component\HttpFoundation\Response `` class or an integer status code.
305296
306- You can also set the default templating engine to something different than the
307- default of ``twig ``:
308-
309- .. code-block :: yaml
310-
311- fos_rest :
312- view :
313- formats :
314- rss : true
315- xml : false
316- templating_formats :
317- html : true
318- force_redirects :
319- html : true
320- failed_validation : HTTP_BAD_REQUEST
321- default_engine : twig
322-
323- Custom handler
324- --------------
325-
326- While many things should be possible via the serializer, in some cases
327- it might not be enough. For example you might need some custom logic to be
328- executed in the ``ViewHandler ``. For these cases one might want to register a
329- custom handler for a specific format. The custom handler can either be
330- registered by defining a custom service, via a compiler pass, or it can be
331- registered from inside the controller action.
332-
333- The callable will receive 3 parameters:
334-
335- * the instance of the ``ViewHandler ``
336- * the instance of the ``View ``
337- * the instance of the ``Request ``
338-
339- Note there are several public methods on the ``ViewHandler `` which can be helpful:
340-
341- * ``isFormatTemplating() ``
342- * ``createResponse() ``
343- * ``createRedirectResponse() ``
344- * ``renderTemplate() ``
345-
346- There is an example for how to register a custom handler (for an RSS feed) in ``Resources\doc\examples ``:
347- https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/examples/RssHandler.php
348-
349- Here is an example using a closure registered inside a Controller action:
350-
351- .. code-block :: php
352-
353- <?php
354-
355- namespace AppBundle\Controller;
356-
357- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
358- use FOS\RestBundle\View\View;
359-
360- class UsersController extends Controller
361- {
362- public function getUsersAction()
363- {
364- $view = View::create();
365-
366- // ...
367-
368- $handler = $this->get('fos_rest.view_handler');
369- if (!$handler->isFormatTemplating($view->getFormat())) {
370- $templatingHandler = function ($handler, $view, $request) {
371- // if a template is set, render it using the 'params'
372- // and place the content into the data
373- if ($view->getTemplate()) {
374- $data = $view->getData();
375-
376- if (empty($data['params'])) {
377- $params = array();
378- } else {
379- $params = $data['params'];
380- unset($data['params']);
381- }
382-
383- $view->setData($params);
384- $data['html'] = $handler->renderTemplate($view, 'html');
385-
386- $view->setData($data);
387- }
388-
389- return $handler->createResponse($view, $request, $format);
390- };
391-
392- $handler->registerHandler($view->getFormat(), $templatingHandler);
393- }
394-
395- return $handler->handle($view);
396- }
397- }
398-
399297JSONP custom handler
400298~~~~~~~~~~~~~~~~~~~~
401299
0 commit comments