Skip to content

Commit dcc0c13

Browse files
committed
Merge pull request #576 from merk/fill-default-template-vars
[Proposal] Configuration to disable copying of default template variables
2 parents b4a5155 + 584bfc0 commit dcc0c13

File tree

4 files changed

+227
-114
lines changed

4 files changed

+227
-114
lines changed

Controller/Annotations/View.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class View extends Template
3535
*/
3636
protected $serializerGroups;
3737

38+
/**
39+
* @var Boolean
40+
*/
41+
protected $populateDefaultVars = true;
42+
3843
/**
3944
* Returns the annotation alias name.
4045
*
@@ -97,4 +102,20 @@ public function getSerializerGroups()
97102
{
98103
return $this->serializerGroups;
99104
}
105+
106+
/**
107+
* @param Boolean $populateDefaultVars
108+
*/
109+
public function setPopulateDefaultVars($populateDefaultVars)
110+
{
111+
$this->populateDefaultVars = (Boolean) $populateDefaultVars;
112+
}
113+
114+
/**
115+
* @return Boolean
116+
*/
117+
public function isPopulateDefaultVars()
118+
{
119+
return $this->populateDefaultVars;
120+
}
100121
}

EventListener/ViewResponseListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function onKernelController(FilterControllerEvent $event)
7272
public function onKernelView(GetResponseForControllerResultEvent $event)
7373
{
7474
$request = $event->getRequest();
75+
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
7576
$configuration = $request->attributes->get('_view');
7677

7778
$view = $event->getControllerResult();
@@ -95,14 +96,17 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
9596
$context->setGroups($configuration->getSerializerGroups());
9697
$view->setSerializationContext($context);
9798
}
99+
$populateDefaultVars = $configuration->isPopulateDefaultVars();
100+
} else {
101+
$populateDefaultVars = true;
98102
}
99103

100104
if (null === $view->getFormat()) {
101105
$view->setFormat($request->getRequestFormat());
102106
}
103107

104108
$vars = $request->attributes->get('_template_vars');
105-
if (!$vars) {
109+
if (!$vars && $populateDefaultVars) {
106110
$vars = $request->attributes->get('_template_default_vars');
107111
}
108112

@@ -133,4 +137,4 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
133137

134138
$event->setResponse($response);
135139
}
136-
}
140+
}

Resources/doc/3-listener-support.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ public function getUsersAction()
144144
See the following example code for more details:
145145
https://github.com/liip/LiipHelloBundle/blob/master/Controller/ExtraController.php
146146

147+
The ViewResponse listener will automatically populate your view with request attributes if
148+
you do not provide any data when returning a view object. This behaviour comes from
149+
SensioFrameworkExtraBundle and will automatically add any variables listed in the
150+
`_template_default_vars` request attribute when no data is supplied. In some cases, this
151+
is not desirable and can be disabled by either supplying the data you want or disabling
152+
the automatic population of data with the @View annotation. :
153+
154+
```php
155+
/**
156+
* $user will no longer end up in the View's data.
157+
*
158+
* @View(populateDefaultVars=false)
159+
*/
160+
public function getUserDetails(User $user)
161+
{
162+
}
163+
```
164+
147165
### Body listener
148166

149167
The Request body decoding listener makes it possible to decode the contents of
@@ -195,7 +213,7 @@ fos_rest:
195213
```
196214
197215
Note: You will probably want to disable the automatic route generation (`@NoRoute`)
198-
for routes using the body converter, and instead define the routes manually to
216+
for routes using the body converter, and instead define the routes manually to
199217
avoid having the deserialized, typehinted objects (`$post in this example`) appear
200218
in the route as a parameter.
201219

0 commit comments

Comments
 (0)