Skip to content

[FrameworkBundle][DX] Improving the redirect config when using RedirectController #33217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,23 @@ public function urlRedirectAction(Request $request, string $path, bool $permanen

return new RedirectResponse($url, $statusCode);
}

public function __invoke(Request $request): Response
{
$p = $request->attributes->get('_route_params', []);

if (\array_key_exists('route', $p)) {
if (\array_key_exists('path', $p)) {
throw new \RuntimeException(sprintf('Ambiguous redirection settings, use the "path" or "route" parameter, not both: "%s" and "%s" found respectively in "%s" routing configuration.', $p['path'], $p['route'], $request->attributes->get('_route')));
}

return $this->redirectAction($request, $p['route'], $p['permanent'] ?? false, $p['ignoreAttributes'] ?? false, $p['keepRequestMethod'] ?? false, $p['keepQueryParams'] ?? false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for all the common parameters, you could make them arguments of the controller instead, to let Symfony bind them (with any logic applied by argument resolver, as done for other methods) and take the default values from the method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's worth it just for two common parameters.

}

if (\array_key_exists('path', $p)) {
return $this->urlRedirectAction($request, $p['path'], $p['permanent'] ?? false, $p['scheme'] ?? null, $p['httpPort'] ?? null, $p['httpsPort'] ?? null, $p['keepRequestMethod'] ?? false);
}

throw new \RuntimeException(sprintf('The parameter "path" or "route" is required to configure the redirect action in "%s" routing configuration.', $request->attributes->get('_route')));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ public function testEmptyRoute()
} catch (HttpException $e) {
$this->assertSame(404, $e->getStatusCode());
}

$request = new Request([], [], ['_route_params' => ['route' => '', 'permanent' => true]]);
try {
$controller($request);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(410, $e->getStatusCode());
}

$request = new Request([], [], ['_route_params' => ['route' => '', 'permanent' => false]]);
try {
$controller($request);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(404, $e->getStatusCode());
}
}

/**
Expand Down Expand Up @@ -71,15 +87,18 @@ public function testRoute($permanent, $keepRequestMethod, $keepQueryParams, $ign

$router = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
$router
->expects($this->once())
->expects($this->exactly(2))
->method('generate')
->with($this->equalTo($route), $this->equalTo($expectedAttributes))
->willReturn($url);

$controller = new RedirectController($router);

$returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes, $keepRequestMethod, $keepQueryParams);
$this->assertRedirectUrl($returnResponse, $url);
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());

$returnResponse = $controller($request);
$this->assertRedirectUrl($returnResponse, $url);
$this->assertEquals($expectedCode, $returnResponse->getStatusCode());
}
Expand Down Expand Up @@ -116,14 +135,35 @@ public function testEmptyPath()
} catch (HttpException $e) {
$this->assertSame(404, $e->getStatusCode());
}

$request = new Request([], [], ['_route_params' => ['path' => '', 'permanent' => true]]);
try {
$controller($request);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(410, $e->getStatusCode());
}

$request = new Request([], [], ['_route_params' => ['path' => '', 'permanent' => false]]);
try {
$controller($request);
$this->fail('Expected Symfony\Component\HttpKernel\Exception\HttpException to be thrown');
} catch (HttpException $e) {
$this->assertSame(404, $e->getStatusCode());
}
}

public function testFullURL()
{
$request = new Request();
$controller = new RedirectController();

$returnResponse = $controller->urlRedirectAction($request, 'http://foo.bar/');
$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
$this->assertEquals(302, $returnResponse->getStatusCode());

$request = new Request([], [], ['_route_params' => ['path' => 'http://foo.bar/']]);
$returnResponse = $controller($request);
$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
$this->assertEquals(302, $returnResponse->getStatusCode());
}
Expand All @@ -132,8 +172,13 @@ public function testFullURLWithMethodKeep()
{
$request = new Request();
$controller = new RedirectController();

$returnResponse = $controller->urlRedirectAction($request, 'http://foo.bar/', false, null, null, null, true);
$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
$this->assertEquals(307, $returnResponse->getStatusCode());

$request = new Request([], [], ['_route_params' => ['path' => 'http://foo.bar/', 'keepRequestMethod' => true]]);
$returnResponse = $controller($request);
$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
$this->assertEquals(307, $returnResponse->getStatusCode());
}
Expand All @@ -151,12 +196,18 @@ public function testUrlRedirectDefaultPorts()
$controller = $this->createRedirectController(null, $httpsPort);
$returnValue = $controller->urlRedirectAction($request, $path, false, 'https');
$this->assertRedirectUrl($returnValue, $expectedUrl);
$request->attributes = new ParameterBag(['_route_params' => ['path' => $path, 'scheme' => 'https']]);
$returnValue = $controller($request);
$this->assertRedirectUrl($returnValue, $expectedUrl);

$expectedUrl = "http://$host:$httpPort$baseUrl$path";
$request = $this->createRequestObject('https', $host, $httpPort, $baseUrl);
$controller = $this->createRedirectController($httpPort);
$returnValue = $controller->urlRedirectAction($request, $path, false, 'http');
$this->assertRedirectUrl($returnValue, $expectedUrl);
$request->attributes = new ParameterBag(['_route_params' => ['path' => $path, 'scheme' => 'http']]);
$returnValue = $controller($request);
$this->assertRedirectUrl($returnValue, $expectedUrl);
}

public function urlRedirectProvider()
Expand Down Expand Up @@ -205,6 +256,10 @@ public function testUrlRedirect($scheme, $httpPort, $httpsPort, $requestScheme,

$returnValue = $controller->urlRedirectAction($request, $path, false, $scheme, $httpPort, $httpsPort);
$this->assertRedirectUrl($returnValue, $expectedUrl);

$request->attributes = new ParameterBag(['_route_params' => ['path' => $path, 'scheme' => $scheme, 'httpPort' => $httpPort, 'httpsPort' => $httpsPort]]);
$returnValue = $controller($request);
$this->assertRedirectUrl($returnValue, $expectedUrl);
}

public function pathQueryParamsProvider()
Expand Down Expand Up @@ -234,6 +289,10 @@ public function testPathQueryParams($expectedUrl, $path, $queryString)

$returnValue = $controller->urlRedirectAction($request, $path, false, $scheme, $port, null);
$this->assertRedirectUrl($returnValue, $expectedUrl);

$request->attributes = new ParameterBag(['_route_params' => ['path' => $path, 'scheme' => $scheme, 'httpPort' => $port]]);
$returnValue = $controller($request);
$this->assertRedirectUrl($returnValue, $expectedUrl);
}

public function testRedirectWithQuery()
Expand All @@ -247,10 +306,13 @@ public function testRedirectWithQuery()
$request->query = new ParameterBag(['base' => 'zaza']);
$request->attributes = new ParameterBag(['_route_params' => ['base2' => 'zaza']]);
$urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
$urlGenerator->expects($this->once())->method('generate')->willReturn('/test?base=zaza&base2=zaza')->with('/test', ['base' => 'zaza', 'base2' => 'zaza'], UrlGeneratorInterface::ABSOLUTE_URL);
$urlGenerator->expects($this->exactly(2))->method('generate')->willReturn('/test?base=zaza&base2=zaza')->with('/test', ['base' => 'zaza', 'base2' => 'zaza'], UrlGeneratorInterface::ABSOLUTE_URL);

$controller = new RedirectController($urlGenerator);
$this->assertRedirectUrl($controller->redirectAction($request, '/test', false, false, false, true), '/test?base=zaza&base2=zaza');

$request->attributes->set('_route_params', ['base2' => 'zaza', 'route' => '/test', 'ignoreAttributes' => false, 'keepRequestMethod' => false, 'keepQueryParams' => true]);
$this->assertRedirectUrl($controller($request), '/test?base=zaza&base2=zaza');
}

public function testRedirectWithQueryWithRouteParamsOveriding()
Expand All @@ -264,10 +326,29 @@ public function testRedirectWithQueryWithRouteParamsOveriding()
$request->query = new ParameterBag(['base' => 'zaza']);
$request->attributes = new ParameterBag(['_route_params' => ['base' => 'zouzou']]);
$urlGenerator = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
$urlGenerator->expects($this->once())->method('generate')->willReturn('/test?base=zouzou')->with('/test', ['base' => 'zouzou'], UrlGeneratorInterface::ABSOLUTE_URL);
$urlGenerator->expects($this->exactly(2))->method('generate')->willReturn('/test?base=zouzou')->with('/test', ['base' => 'zouzou'], UrlGeneratorInterface::ABSOLUTE_URL);

$controller = new RedirectController($urlGenerator);
$this->assertRedirectUrl($controller->redirectAction($request, '/test', false, false, false, true), '/test?base=zouzou');

$request->attributes->set('_route_params', ['base' => 'zouzou', 'route' => '/test', 'ignoreAttributes' => false, 'keepRequestMethod' => false, 'keepQueryParams' => true]);
$this->assertRedirectUrl($controller($request), '/test?base=zouzou');
}

public function testMissingPathOrRouteParameter()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('The parameter "path" or "route" is required to configure the redirect action in "_redirect" routing configuration.');

(new RedirectController())(new Request([], [], ['_route' => '_redirect']));
}

public function testAmbiguousPathAndRouteParameter()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Ambiguous redirection settings, use the "path" or "route" parameter, not both: "/foo" and "bar" found respectively in "_redirect" routing configuration.');

(new RedirectController())(new Request([], [], ['_route' => '_redirect', '_route_params' => ['path' => '/foo', 'route' => 'bar']]));
}

private function createRequestObject($scheme, $host, $port, $baseUrl, $queryString = '')
Expand Down