Skip to content

[Routing] Add matched and default parameters to redirect responses #19037

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

Closed
Closed
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 @@ -32,7 +32,7 @@ public function testRedirectWhenNoSlash()
'scheme' => null,
'httpPort' => $context->getHttpPort(),
'httpsPort' => $context->getHttpsPort(),
'_route' => null,
'_route' => 'foo',
),
$matcher->match('/foo')
);
Expand Down
48 changes: 25 additions & 23 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,32 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
}
}

// optimize parameters array
if ($matches || $hostMatches) {
$vars = array();
if ($hostMatches) {
$vars[] = '$hostMatches';
}
if ($matches) {
$vars[] = '$matches';
}
$vars[] = "array('_route' => '$name')";

$code .= sprintf(
" \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n",
implode(', ', $vars),
str_replace("\n", '', var_export($route->getDefaults(), true))
);
} elseif ($route->getDefaults()) {
$code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
} else {
$code .= sprintf(" \$ret = array('_route' => '%s');\n", $name);
}

if ($hasTrailingSlash) {
$code .= <<<EOF
if (substr(\$pathinfo, -1) !== '/') {
return \$this->redirect(\$pathinfo.'/', '$name');
return array_replace(\$ret, \$this->redirect(\$pathinfo.'/', '$name'));
}


Expand All @@ -304,34 +326,14 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
$code .= <<<EOF
\$requiredSchemes = $schemes;
if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes));
return array_replace(\$ret, \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes)));
}


EOF;
}

// optimize parameters array
if ($matches || $hostMatches) {
$vars = array();
if ($hostMatches) {
$vars[] = '$hostMatches';
}
if ($matches) {
$vars[] = '$matches';
}
$vars[] = "array('_route' => '$name')";

$code .= sprintf(
" return \$this->mergeDefaults(array_replace(%s), %s);\n",
implode(', ', $vars),
str_replace("\n", '', var_export($route->getDefaults(), true))
);
} elseif ($route->getDefaults()) {
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
} else {
$code .= sprintf(" return array('_route' => '%s');\n", $name);
}
$code .= " return \$ret;\n";
$code .= " }\n";

if ($methods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function match($pathinfo)
}

try {
parent::match($pathinfo.'/');
$parameters = parent::match($pathinfo.'/');

return $this->redirect($pathinfo.'/', null);
return array_replace($parameters, $this->redirect($pathinfo.'/', isset($parameters['_route']) ? $parameters['_route'] : null));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passing the _route to the redirect method makes the tests fail in the FrameworkBundle: https://github.com/symfony/symfony/pull/19037/files#diff-7b7153654c7ee1d563e8421f3f9be20e

I could extract this fix as a bugfix as the current behavior is not consistent with the dumped matcher, see https://github.com/symfony/symfony/pull/19037/files#diff-3b72491a9ba1cff58442b845ae837eb3L292 which passed the route name as second argument to redirect.

But I don't know how to fix the frameworkbundle test which will also be run against the lowest allowed routing version, right? So I need to increase the routing dependency in https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bundle/FrameworkBundle/composer.json#L28 but to a version that is not released yet?

Copy link
Member

Choose a reason for hiding this comment

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

right, @nicolas-grekas can you help @Tobion with the best composer changes to do here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas I could still use your help here

Copy link
Member

Choose a reason for hiding this comment

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

to a version that is not released yet

yes, that works, wouldn't be the first time

Copy link
Contributor Author

@Tobion Tobion Oct 31, 2016

Choose a reason for hiding this comment

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

So "symfony/routing": "~2.7.21|~2.8.14|^3.1.7"? I don't see how composer will know to install the latest 2.7 branch then as 2.7.21 is not released yet. And even if it does, when the PR is not merged yet, the fix won't be installed. Thus the tests cannot pass before they are merged?

Copy link
Member

@nicolas-grekas nicolas-grekas Nov 9, 2016

Choose a reason for hiding this comment

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

Sorry, late answer. Yes, it will work, composer will know thanks to the branch-alias in composer.json

Copy link
Member

@nicolas-grekas nicolas-grekas Nov 9, 2016

Choose a reason for hiding this comment

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

The PR doesn't need to be merged: a local composer repository is made on travis to simulate the merge before it really happens. See .github/build-packages.php

} catch (ResourceNotFoundException $e2) {
throw $e;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)

$status = $this->handleRouteRequirements($pathinfo, $name, $route);

if (self::ROUTE_MATCH === $status[0]) {
return $status[1];
}

if (self::REQUIREMENT_MISMATCH === $status[0]) {
continue;
}

return $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : array()));
}
}

Expand Down
Loading