Permalink
Browse files

set view_response_listener to 'force' by default

  • Loading branch information...
1 parent bec85e4 commit 64489cf84c324698518b2df4c0d81c41ce90949a @lsmith77 lsmith77 committed Sep 2, 2011
Showing with 17 additions and 14 deletions.
  1. +1 −1 DependencyInjection/Configuration.php
  2. +16 −13 README.md
@@ -67,7 +67,7 @@ public function getConfigTreeBuilder()
->defaultValue(array('html' => true))
->prototype('boolean')->end()
->end()
- ->scalarNode('view_response_listener')->defaultTrue()->end()
+ ->scalarNode('view_response_listener')->defaultvalue('force')->end()
@stof

stof Sep 2, 2011

Owner

you should use an uppercased V to match the case of the method in the class.

@lsmith77

lsmith77 Sep 2, 2011

Owner

i think i need to replace my keyboard .. shift is not triggering all too often these days.

->scalarNode('failed_validation')->defaultValue(Codes::HTTP_BAD_REQUEST)->end()
->end()
->end()
View
@@ -287,6 +287,9 @@ For example, below you can see how to disable the body listener:
# app/config/config.yml
fos_rest:
body_listener: false
+ format_listener: false
+ view:
+ view_response_listener: false
```
### View Response listener
@@ -324,8 +327,18 @@ class UsersController
As this feature is heavily based on the SensioFrameworkBundle, the example can further be
simplified by using the various annotations supported by that bundle. There is also one
-additional annotation called ``@View()`` which extends from the ``@Template()`` annotation
-but also instructs the view listener to automatically create a ``View`` instance if necessary.
+additional annotation called ``@View()`` which extends from the ``@Template()`` annotation.
+The ``@View()`` and ``@Template()`` annotations essentially behave the same with a minor
+difference. In order to fallback to SensioFrameworkBundle's handling whenever no
+``View`` instance is returned and ``@View()`` isn't its necessary to change the
+``view_response_listener`` setting from the default of ``force`` to just ``true``.
+
+```yaml
+# app/config/config.yml
+fos_rest:
+ view:
+ view_response_listener: true
+```
Note, ``@View()`` currently only works if the following patch is applied:
https://github.com/sensio/SensioFrameworkExtraBundle/pull/57
@@ -349,16 +362,6 @@ class UsersController
}
```
-In case even if no ``View`` instance is returned and ``@View()`` isn't used its possible
-to automatically get a ``View`` instance created by setting the ``force`` option.
-
-```yaml
-# app/config/config.yml
-fos_rest:
- view:
- view_response_listener: force
-```
-
See the following example code for more details:
https://github.com/liip/LiipHelloBundle/blob/master/Controller/ExtraController.php
@@ -753,7 +756,7 @@ fos_rest:
xml: true
templating_formats:
html: true
- view_response_listener: true
+ view_response_listener: 'force'
failed_validation: HTTP_BAD_REQUEST
exception:
codes: ~

13 comments on commit 64489cf

Owner

stof replied Sep 2, 2011

Also what about renaming the other possible values ? true seems weird as opposed to 'force'. Also, you should add a validate call in the Configuration class to allow only the valid values for the node.

Owner

lsmith77 replied Sep 2, 2011

What do you mean with "other possible values"? Do you mean only supporting false and two string values rather than true and a string value?

Owner

stof replied Sep 2, 2011

yes. true does not really describe what the setting does whereas the other 2 are explicit: false => disabling (it is used to disable things in many places in Sf2 as it is the value eventually used to unset a node) and "force" -> always creating a View

Owner

lsmith77 replied Sep 2, 2011

i cant really think of a good name. first i wanted to use true for what is now force and use a string to disable forcing. then again the question is if i even need to differentiate. aka is it ok if RestBundle provides no way to let the SensioFrameworkBundle continuing handling the rendering?

Owner

stof replied Sep 2, 2011

I think it should be better to let the user choose whether he wants to force or no. What he wants to use FOSRestBundle only for parts of the project (an API for instance) and use SensioFrameworkExtraBundle for another part. Will the result be the same if FOSRestBundle forces to use its listener ?

Also, how are you choosing the template for HTML pages when using force ? I thought the logic was done by the @View annotation ?

Owner

lsmith77 replied Sep 2, 2011

@View() extends @Template(), so one can set a template via @View(). The only difference right now is that @View() will cause the creation of a View instance even if view_response_listener is set to true rather than the default force.

Owner

stof replied Sep 2, 2011

but what about the case of using force ? will they have to use @Template in this case ?

Owner

lsmith77 replied Sep 2, 2011

err .. if they use @Template() then use they have to use force

Owner

stof replied Sep 2, 2011

My question is, do they need to use @Template or @View when using force or not ?

Owner

lsmith77 replied Sep 2, 2011

no .. this is the logic:

if (!$view instanceOf View) {
    if (!$request->attributes->get('_view') && !$this->container->getParameter('fos_rest.view_reponse_listener.force_view')) {
        return;
    }

    $view = new View($view);
}
Owner

stof replied Sep 2, 2011

and in this case, how do you find the template for template-aware formats ?

Owner

lsmith77 replied Sep 2, 2011

i cant .. its just like manually doing everything and not setting a template.

Owner

stof replied Sep 2, 2011

so templating-aware formats will still depend on SensioFrameworkExtraBundle.

Please sign in to comment.