Skip to content

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

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
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 @@ -33,7 +33,7 @@ public function testRedirectWhenNoSlash()
'scheme' => null,
'httpPort' => $context->getHttpPort(),
'httpsPort' => $context->getHttpsPort(),
'_route' => null,
'_route' => 'foo',
),
$matcher->match('/foo')
);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added support for prioritized routing loaders.
* Add matched and default parameters to redirect responses

3.3.0
-----
Expand Down
51 changes: 30 additions & 21 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,35 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
}
}

// the offset where the return value is appended below, with indendation
$retOffset = 12 + strlen($code);

// 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 @@ -351,33 +376,17 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
$code .= <<<EOF
\$requiredSchemes = $schemes;
if (!isset(\$requiredSchemes[\$scheme])) {
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)));
if ($hasTrailingSlash || $schemes) {
$code .= " return \$ret;\n";
} else {
$code .= sprintf(" return array('_route' => '%s');\n", $name);
$code = substr_replace($code, 'return', $retOffset, 6);
}
$code .= " }\n";

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));
} 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
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,24 @@ public function match($pathinfo)

// baz3
if ('/test/baz3' === $trimmedPathinfo) {
$ret = array('_route' => 'baz3');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'baz3');
return array_replace($ret, $this->redirect($pathinfo.'/', 'baz3'));
}

return array('_route' => 'baz3');
return $ret;
}

}

// baz4
if (preg_match('#^/test/(?P<foo>[^/]++)/?$#s', $pathinfo, $matches)) {
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'baz4');
return array_replace($ret, $this->redirect($pathinfo.'/', 'baz4'));
}

return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
return $ret;
}

// baz5
Expand Down Expand Up @@ -181,11 +183,12 @@ public function match($pathinfo)

// hey
if ('/multi/hey' === $trimmedPathinfo) {
$ret = array('_route' => 'hey');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'hey');
return array_replace($ret, $this->redirect($pathinfo.'/', 'hey'));
}

return array('_route' => 'hey');
return $ret;
}

// overridden2
Expand Down Expand Up @@ -326,22 +329,24 @@ public function match($pathinfo)

// secure
if ('/secure' === $pathinfo) {
$ret = array('_route' => 'secure');
$requiredSchemes = array ( 'https' => 0,);
if (!isset($requiredSchemes[$scheme])) {
return $this->redirect($pathinfo, 'secure', key($requiredSchemes));
return array_replace($ret, $this->redirect($pathinfo, 'secure', key($requiredSchemes)));
}

return array('_route' => 'secure');
return $ret;
}

// nonsecure
if ('/nonsecure' === $pathinfo) {
$ret = array('_route' => 'nonsecure');
$requiredSchemes = array ( 'http' => 0,);
if (!isset($requiredSchemes[$scheme])) {
return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes));
return array_replace($ret, $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)));
}

return array('_route' => 'nonsecure');
return $ret;
}

throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,32 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/a')) {
// a_fourth
if ('/a/44' === $trimmedPathinfo) {
$ret = array('_route' => 'a_fourth');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'a_fourth');
return array_replace($ret, $this->redirect($pathinfo.'/', 'a_fourth'));
}

return array('_route' => 'a_fourth');
return $ret;
}

// a_fifth
if ('/a/55' === $trimmedPathinfo) {
$ret = array('_route' => 'a_fifth');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'a_fifth');
return array_replace($ret, $this->redirect($pathinfo.'/', 'a_fifth'));
}

return array('_route' => 'a_fifth');
return $ret;
}

// a_sixth
if ('/a/66' === $trimmedPathinfo) {
$ret = array('_route' => 'a_sixth');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'a_sixth');
return array_replace($ret, $this->redirect($pathinfo.'/', 'a_sixth'));
}

return array('_route' => 'a_sixth');
return $ret;
}

}
Expand All @@ -96,59 +99,65 @@ public function match($pathinfo)
if (0 === strpos($pathinfo, '/nested/group')) {
// nested_a
if ('/nested/group/a' === $trimmedPathinfo) {
$ret = array('_route' => 'nested_a');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'nested_a');
return array_replace($ret, $this->redirect($pathinfo.'/', 'nested_a'));
}

return array('_route' => 'nested_a');
return $ret;
}

// nested_b
if ('/nested/group/b' === $trimmedPathinfo) {
$ret = array('_route' => 'nested_b');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'nested_b');
return array_replace($ret, $this->redirect($pathinfo.'/', 'nested_b'));
}

return array('_route' => 'nested_b');
return $ret;
}

// nested_c
if ('/nested/group/c' === $trimmedPathinfo) {
$ret = array('_route' => 'nested_c');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'nested_c');
return array_replace($ret, $this->redirect($pathinfo.'/', 'nested_c'));
}

return array('_route' => 'nested_c');
return $ret;
}

}

elseif (0 === strpos($pathinfo, '/slashed/group')) {
// slashed_a
if ('/slashed/group' === $trimmedPathinfo) {
$ret = array('_route' => 'slashed_a');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'slashed_a');
return array_replace($ret, $this->redirect($pathinfo.'/', 'slashed_a'));
}

return array('_route' => 'slashed_a');
return $ret;
}

// slashed_b
if ('/slashed/group/b' === $trimmedPathinfo) {
$ret = array('_route' => 'slashed_b');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'slashed_b');
return array_replace($ret, $this->redirect($pathinfo.'/', 'slashed_b'));
}

return array('_route' => 'slashed_b');
return $ret;
}

// slashed_c
if ('/slashed/group/c' === $trimmedPathinfo) {
$ret = array('_route' => 'slashed_c');
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'slashed_c');
return array_replace($ret, $this->redirect($pathinfo.'/', 'slashed_c'));
}

return array('_route' => 'slashed_c');
return $ret;
}

}
Expand Down
Loading