Skip to content

Commit 7747c14

Browse files
committed
Merge pull request #1250 from FriendsOfSymfony/fix_issue_with_redirect
Fix issue with redirect
2 parents 9be4952 + 06a3d49 commit 7747c14

File tree

8 files changed

+113
-2
lines changed

8 files changed

+113
-2
lines changed

EventListener/ViewResponseListener.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use JMS\Serializer\SerializationContext;
1919
use FOS\RestBundle\View\View;
2020
use FOS\RestBundle\Util\Codes;
21+
use FOS\RestBundle\View\ViewHandlerInterface;
2122

2223
/**
2324
* The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation.
@@ -97,11 +98,13 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
9798
$vars = $request->attributes->get('_template_default_vars');
9899
}
99100

101+
/** @var ViewHandlerInterface $viewHandler */
100102
$viewHandler = $this->container->get('fos_rest.view_handler');
101103

102104
if ($viewHandler->isFormatTemplating($view->getFormat())) {
103105
if (!empty($vars)) {
104106
$parameters = (array) $viewHandler->prepareTemplateParameters($view);
107+
105108
foreach ($vars as $var) {
106109
if (!array_key_exists($var, $parameters)) {
107110
$parameters[$var] = $request->attributes->get($var);
@@ -111,7 +114,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
111114
}
112115

113116
$template = $request->attributes->get('_template');
114-
if ($template) {
117+
if ($template && !$view->getTemplate()) {
115118
if ($template instanceof TemplateReference) {
116119
$template->set('format', null);
117120
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use FOS\RestBundle\Controller\Annotations\RouteResource;
16+
use FOS\RestBundle\Controller\Annotations\View;
17+
use FOS\RestBundle\Controller\FOSRestController;
18+
19+
/**
20+
* @RouteResource("Article")
21+
*/
22+
class ArticleController extends FOSRestController
23+
{
24+
/**
25+
* Get list.
26+
*
27+
* @param Request $request
28+
*
29+
* @return View view instance
30+
*
31+
* @View()
32+
*/
33+
public function cgetAction(Request $request)
34+
{
35+
$view = $this->view();
36+
$view->setTemplate('TestBundle:Article:foo.html.twig');
37+
38+
return $view;
39+
}
40+
}

Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ test_param_fetcher:
1717
test_param_fetcher_test:
1818
path: /params/test
1919
defaults: { _controller: TestBundle:ParamFetcher:test }
20+
21+
test_view_response_listener:
22+
resource: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\ArticleController
23+
type: rest
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fooo
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Functional;
13+
14+
class ViewResponseListenerTest extends WebTestCase
15+
{
16+
public function testTemplateOverride()
17+
{
18+
$client = $this->createClient(array('test_case' => 'ViewResponseListener'));
19+
$client->request(
20+
'GET',
21+
'/articles'
22+
);
23+
24+
$this->assertSame(200, $client->getResponse()->getStatusCode());
25+
$this->assertContains('fooo', $client->getResponse()->getContent());
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
return array(
13+
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
14+
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
15+
new \FOS\RestBundle\FOSRestBundle(),
16+
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
17+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
serializer:
6+
enabled: true
7+
8+
fos_rest:
9+
view:
10+
view_response_listener: 'force'
11+
formats:
12+
xml: true
13+
json: true
14+
templating_formats:
15+
html: true
16+
format_listener:
17+
rules:
18+
- { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }

View/ViewHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
2222
use Symfony\Component\Form\FormInterface;
2323
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
24+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2425
use FOS\RestBundle\Util\Codes;
2526

2627
/**
@@ -430,7 +431,7 @@ public function createResponse(View $view, Request $request, $format)
430431
{
431432
$route = $view->getRoute();
432433
$location = $route
433-
? $this->getRouter()->generate($route, (array) $view->getRouteParameters(), true)
434+
? $this->getRouter()->generate($route, (array) $view->getRouteParameters(), UrlGeneratorInterface::ABSOLUTE_URL)
434435
: $view->getLocation();
435436

436437
if ($location) {

0 commit comments

Comments
 (0)