Skip to content

Commit 91fe647

Browse files
committed
minor #2252 clean up the documentation a bit (xabbuh)
This PR was merged into the 2.x branch. Discussion ---------- clean up the documentation a bit Commits ------- 7de9ac0 clean up the documentation a bit
2 parents 2da2b18 + 7de9ac0 commit 91fe647

File tree

6 files changed

+7
-323
lines changed

6 files changed

+7
-323
lines changed

Resources/doc/2-the-view-layer.rst

Lines changed: 6 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Introduction
66

77
The view layer makes it possible to write ``format`` (html, json, xml, etc)
88
agnostic 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

1111
The bundle works both with the `Symfony Serializer Component`_ and the more
1212
sophisticated `serializer`_ created by Johannes Schmitt and integrated via the
@@ -134,17 +134,11 @@ combination with SensioFrameworkExtraBundle you can even omit the calls to
134134

135135
As 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

143139
There 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

149143
There are several more methods on the ``View`` class, here is a list of all
150144
the important ones for configuring the view:
@@ -288,12 +282,9 @@ the task without any client modifications.
288282
Configuration
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

298289
When using ``RouteRedirectView::create()`` the default behavior of forcing a
299290
redirect 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-
399297
JSONP custom handler
400298
~~~~~~~~~~~~~~~~~~~~
401299

Resources/doc/3-listener-support.rst

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -160,39 +160,6 @@ You need to enable this listener as follows, as it is disabled by default:
160160
fos_rest:
161161
allowed_methods_listener: true
162162
163-
Security Exception Listener
164-
---------------------------
165-
166-
By default it is the responsibility of firewall access points to deal with
167-
AccessDeniedExceptions. For example the ``form`` entry point will redirect to
168-
the login page. However, for a RESTful application proper response HTTP status
169-
codes should be provided. This listener is triggered before the normal exception
170-
listener and firewall entry points and forces returning either a 403 or 401
171-
status code for any of the formats configured.
172-
173-
It will return 401 for
174-
``Symfony\Component\Security\Core\Exception\AuthenticationException`` or 403 for
175-
``Symfony\Component\Security\Core\Exception\AccessDeniedException``.
176-
177-
As a 401-response requires an authentication-challenge, you can set one using
178-
the configuration ``unauthorized_challenge`` or leave it blank if you don't want
179-
to send a challenge in the ``WWW-Authenticate`` header to the client.
180-
181-
If you want to use an advanced value in this header, it's worth looking at this:
182-
`Test Cases for HTTP Test Cases for the HTTP WWW-Authenticate header field`_.
183-
184-
You need to enable this listener as follows, as it is disabled by default:
185-
186-
.. code-block:: yaml
187-
188-
fos_rest:
189-
unauthorized_challenge: "Basic realm=\"Restricted Area\""
190-
access_denied_listener:
191-
# all requests using the 'json' format will return a 403 on an access denied violation
192-
json: true
193-
194-
Note: The access_denied_listener doesn't return a response itself and must be coupled with an exception listener returning a response (see the :doc:`FOSRestBundle exception controller <4-exception-controller-support>` or the `twig exception controller`_).
195-
196163
Zone Listener
197164
-------------
198165

Resources/doc/4-exception-controller-support.rst

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,6 @@
11
Step 4: ExceptionController support
22
===================================
33

4-
When implementing an API it is also necessary to handle exceptions in a RESTful
5-
way, while ensuring that no security sensitive information leaks out. This
6-
bundle provides an extra controller for that job. Using this custom
7-
ExceptionController it is possible to leverage the View layer when building
8-
responses for uncaught Exceptions.
9-
10-
The ExceptionController can be enabled via the FOSRestBundle
11-
configuration:
12-
13-
.. code-block:: yaml
14-
15-
fos_rest:
16-
exception:
17-
enabled: true
18-
exception_controller: 'Acme\DemoBundle\Controller\ExceptionController::showAction'
19-
20-
.. note::
21-
22-
The FOSRestBundle ExceptionController is executed before the one of the TwigBundle.
23-
24-
.. note::
25-
26-
FOSRestBundle defines two services for exception rendering, by default it
27-
configures ``fos_rest.exception.controller`` which only supports rendering
28-
via a serializer. In case no explicit controller is configured by the user
29-
and TwigBundle is detected it will automatically configure
30-
``fos_rest.exception.twig_controller`` which additionally also supports
31-
rendering via Twig.
32-
334
To map Exception classes to HTTP response status codes an *exception map* may
345
be configured, where the keys match a fully qualified class name and the values
356
are either an integer HTTP response status code or a string matching a class

Resources/doc/configuration-reference.rst

Lines changed: 0 additions & 136 deletions
This file was deleted.

Resources/doc/index.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Getting Started With FOSRestBundle
1616
param_fetcher_listener
1717
4-exception-controller-support
1818
annotations-reference
19-
configuration-reference
2019

2120
Installation
2221
------------
@@ -42,7 +41,7 @@ FOSRestBundle provides several tools to assist in building REST applications:
4241
Config reference
4342
----------------
4443

45-
- :doc:`Configuration reference <configuration-reference>` for a reference on
44+
- Run ``bin/console config:dump-reference fos_rest`` for a reference of
4645
the available configuration options
4746
- :doc:`Annotations reference <annotations-reference>` for a reference on
4847
the available configurations through annotations

Resources/doc/view_response_listener.rst

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,6 @@ you should return a ``$view`` object with the data set by ``setTemplateData``.
8989
return $view;
9090
}
9191
92-
If ``@View()`` is used, the template variable name used to render templating
93-
formats can be configured (default ``'data'``):
94-
95-
.. code-block:: php
96-
97-
<?php
98-
99-
/**
100-
* @View(templateVar="users")
101-
*/
102-
public function getUsersAction()
103-
{
104-
// ...
105-
}
106-
10792
The status code of the view can also be configured:
10893

10994
.. code-block:: php

0 commit comments

Comments
 (0)