Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38905d3

Browse files
feature #23440 [Routing] Add matched and default parameters to redirect responses (artursvonda, Tobion)
This PR was merged into the 3.4 branch. Discussion ---------- [Routing] Add matched and default parameters to redirect responses | Q | A | | --- | --- | | Branch? | master | | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | | Fixed tickets | #18012, #19037 | | License | MIT | | Doc PR | | Finished #18012 and #19037 Commits ------- 4c1fdd4 [Routing] also add matched params for redirect due to trailing slash dc3f7a9 [Routing] Add matched and default parameters to redirect responses
2 parents 9b8d96b + 4c1fdd4 commit 38905d3

File tree

9 files changed

+126
-71
lines changed

9 files changed

+126
-71
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testRedirectWhenNoSlash()
3333
'scheme' => null,
3434
'httpPort' => $context->getHttpPort(),
3535
'httpsPort' => $context->getHttpsPort(),
36-
'_route' => null,
36+
'_route' => 'foo',
3737
),
3838
$matcher->match('/foo')
3939
);

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

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

910
3.3.0
1011
-----

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,35 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
333333
}
334334
}
335335

336+
// the offset where the return value is appended below, with indendation
337+
$retOffset = 12 + strlen($code);
338+
339+
// optimize parameters array
340+
if ($matches || $hostMatches) {
341+
$vars = array();
342+
if ($hostMatches) {
343+
$vars[] = '$hostMatches';
344+
}
345+
if ($matches) {
346+
$vars[] = '$matches';
347+
}
348+
$vars[] = "array('_route' => '$name')";
349+
350+
$code .= sprintf(
351+
" \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n",
352+
implode(', ', $vars),
353+
str_replace("\n", '', var_export($route->getDefaults(), true))
354+
);
355+
} elseif ($route->getDefaults()) {
356+
$code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
357+
} else {
358+
$code .= sprintf(" \$ret = array('_route' => '%s');\n", $name);
359+
}
360+
336361
if ($hasTrailingSlash) {
337362
$code .= <<<EOF
338363
if (substr(\$pathinfo, -1) !== '/') {
339-
return \$this->redirect(\$pathinfo.'/', '$name');
364+
return array_replace(\$ret, \$this->redirect(\$pathinfo.'/', '$name'));
340365
}
341366
342367
@@ -351,33 +376,17 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
351376
$code .= <<<EOF
352377
\$requiredSchemes = $schemes;
353378
if (!isset(\$requiredSchemes[\$scheme])) {
354-
return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes));
379+
return array_replace(\$ret, \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes)));
355380
}
356381
357382
358383
EOF;
359384
}
360385

361-
// optimize parameters array
362-
if ($matches || $hostMatches) {
363-
$vars = array();
364-
if ($hostMatches) {
365-
$vars[] = '$hostMatches';
366-
}
367-
if ($matches) {
368-
$vars[] = '$matches';
369-
}
370-
$vars[] = "array('_route' => '$name')";
371-
372-
$code .= sprintf(
373-
" return \$this->mergeDefaults(array_replace(%s), %s);\n",
374-
implode(', ', $vars),
375-
str_replace("\n", '', var_export($route->getDefaults(), true))
376-
);
377-
} elseif ($route->getDefaults()) {
378-
$code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
386+
if ($hasTrailingSlash || $schemes) {
387+
$code .= " return \$ret;\n";
379388
} else {
380-
$code .= sprintf(" return array('_route' => '%s');\n", $name);
389+
$code = substr_replace($code, 'return', $retOffset, 6);
381390
}
382391
$code .= " }\n";
383392

src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function match($pathinfo)
3232
}
3333

3434
try {
35-
parent::match($pathinfo.'/');
35+
$parameters = parent::match($pathinfo.'/');
3636

37-
return $this->redirect($pathinfo.'/', null);
37+
return array_replace($parameters, $this->redirect($pathinfo.'/', isset($parameters['_route']) ? $parameters['_route'] : null));
3838
} catch (ResourceNotFoundException $e2) {
3939
throw $e;
4040
}

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
163163

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

166-
if (self::ROUTE_MATCH === $status[0]) {
167-
return $status[1];
168-
}
169-
170166
if (self::REQUIREMENT_MISMATCH === $status[0]) {
171167
continue;
172168
}
173169

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

src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,24 @@ public function match($pathinfo)
8787

8888
// baz3
8989
if ('/test/baz3' === $trimmedPathinfo) {
90+
$ret = array('_route' => 'baz3');
9091
if (substr($pathinfo, -1) !== '/') {
91-
return $this->redirect($pathinfo.'/', 'baz3');
92+
return array_replace($ret, $this->redirect($pathinfo.'/', 'baz3'));
9293
}
9394

94-
return array('_route' => 'baz3');
95+
return $ret;
9596
}
9697

9798
}
9899

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

105-
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
107+
return $ret;
106108
}
107109

108110
// baz5
@@ -181,11 +183,12 @@ public function match($pathinfo)
181183

182184
// hey
183185
if ('/multi/hey' === $trimmedPathinfo) {
186+
$ret = array('_route' => 'hey');
184187
if (substr($pathinfo, -1) !== '/') {
185-
return $this->redirect($pathinfo.'/', 'hey');
188+
return array_replace($ret, $this->redirect($pathinfo.'/', 'hey'));
186189
}
187190

188-
return array('_route' => 'hey');
191+
return $ret;
189192
}
190193

191194
// overridden2
@@ -326,22 +329,24 @@ public function match($pathinfo)
326329

327330
// secure
328331
if ('/secure' === $pathinfo) {
332+
$ret = array('_route' => 'secure');
329333
$requiredSchemes = array ( 'https' => 0,);
330334
if (!isset($requiredSchemes[$scheme])) {
331-
return $this->redirect($pathinfo, 'secure', key($requiredSchemes));
335+
return array_replace($ret, $this->redirect($pathinfo, 'secure', key($requiredSchemes)));
332336
}
333337

334-
return array('_route' => 'secure');
338+
return $ret;
335339
}
336340

337341
// nonsecure
338342
if ('/nonsecure' === $pathinfo) {
343+
$ret = array('_route' => 'nonsecure');
339344
$requiredSchemes = array ( 'http' => 0,);
340345
if (!isset($requiredSchemes[$scheme])) {
341-
return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes));
346+
return array_replace($ret, $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)));
342347
}
343348

344-
return array('_route' => 'nonsecure');
349+
return $ret;
345350
}
346351

347352
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();

src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher5.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,32 @@ public function match($pathinfo)
6161
if (0 === strpos($pathinfo, '/a')) {
6262
// a_fourth
6363
if ('/a/44' === $trimmedPathinfo) {
64+
$ret = array('_route' => 'a_fourth');
6465
if (substr($pathinfo, -1) !== '/') {
65-
return $this->redirect($pathinfo.'/', 'a_fourth');
66+
return array_replace($ret, $this->redirect($pathinfo.'/', 'a_fourth'));
6667
}
6768

68-
return array('_route' => 'a_fourth');
69+
return $ret;
6970
}
7071

7172
// a_fifth
7273
if ('/a/55' === $trimmedPathinfo) {
74+
$ret = array('_route' => 'a_fifth');
7375
if (substr($pathinfo, -1) !== '/') {
74-
return $this->redirect($pathinfo.'/', 'a_fifth');
76+
return array_replace($ret, $this->redirect($pathinfo.'/', 'a_fifth'));
7577
}
7678

77-
return array('_route' => 'a_fifth');
79+
return $ret;
7880
}
7981

8082
// a_sixth
8183
if ('/a/66' === $trimmedPathinfo) {
84+
$ret = array('_route' => 'a_sixth');
8285
if (substr($pathinfo, -1) !== '/') {
83-
return $this->redirect($pathinfo.'/', 'a_sixth');
86+
return array_replace($ret, $this->redirect($pathinfo.'/', 'a_sixth'));
8487
}
8588

86-
return array('_route' => 'a_sixth');
89+
return $ret;
8790
}
8891

8992
}
@@ -96,59 +99,65 @@ public function match($pathinfo)
9699
if (0 === strpos($pathinfo, '/nested/group')) {
97100
// nested_a
98101
if ('/nested/group/a' === $trimmedPathinfo) {
102+
$ret = array('_route' => 'nested_a');
99103
if (substr($pathinfo, -1) !== '/') {
100-
return $this->redirect($pathinfo.'/', 'nested_a');
104+
return array_replace($ret, $this->redirect($pathinfo.'/', 'nested_a'));
101105
}
102106

103-
return array('_route' => 'nested_a');
107+
return $ret;
104108
}
105109

106110
// nested_b
107111
if ('/nested/group/b' === $trimmedPathinfo) {
112+
$ret = array('_route' => 'nested_b');
108113
if (substr($pathinfo, -1) !== '/') {
109-
return $this->redirect($pathinfo.'/', 'nested_b');
114+
return array_replace($ret, $this->redirect($pathinfo.'/', 'nested_b'));
110115
}
111116

112-
return array('_route' => 'nested_b');
117+
return $ret;
113118
}
114119

115120
// nested_c
116121
if ('/nested/group/c' === $trimmedPathinfo) {
122+
$ret = array('_route' => 'nested_c');
117123
if (substr($pathinfo, -1) !== '/') {
118-
return $this->redirect($pathinfo.'/', 'nested_c');
124+
return array_replace($ret, $this->redirect($pathinfo.'/', 'nested_c'));
119125
}
120126

121-
return array('_route' => 'nested_c');
127+
return $ret;
122128
}
123129

124130
}
125131

126132
elseif (0 === strpos($pathinfo, '/slashed/group')) {
127133
// slashed_a
128134
if ('/slashed/group' === $trimmedPathinfo) {
135+
$ret = array('_route' => 'slashed_a');
129136
if (substr($pathinfo, -1) !== '/') {
130-
return $this->redirect($pathinfo.'/', 'slashed_a');
137+
return array_replace($ret, $this->redirect($pathinfo.'/', 'slashed_a'));
131138
}
132139

133-
return array('_route' => 'slashed_a');
140+
return $ret;
134141
}
135142

136143
// slashed_b
137144
if ('/slashed/group/b' === $trimmedPathinfo) {
145+
$ret = array('_route' => 'slashed_b');
138146
if (substr($pathinfo, -1) !== '/') {
139-
return $this->redirect($pathinfo.'/', 'slashed_b');
147+
return array_replace($ret, $this->redirect($pathinfo.'/', 'slashed_b'));
140148
}
141149

142-
return array('_route' => 'slashed_b');
150+
return $ret;
143151
}
144152

145153
// slashed_c
146154
if ('/slashed/group/c' === $trimmedPathinfo) {
155+
$ret = array('_route' => 'slashed_c');
147156
if (substr($pathinfo, -1) !== '/') {
148-
return $this->redirect($pathinfo.'/', 'slashed_c');
157+
return array_replace($ret, $this->redirect($pathinfo.'/', 'slashed_c'));
149158
}
150159

151-
return array('_route' => 'slashed_c');
160+
return $ret;
152161
}
153162

154163
}

0 commit comments

Comments
 (0)