Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5e42162

Browse filesBrowse files
[Routing] Redirect from trailing slash to no-slash when possible
1 parent a38cbd0 commit 5e42162
Copy full SHA for 5e42162
Expand file treeCollapse file tree

18 files changed

+287
-301
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
+68-89Lines changed: 68 additions & 89 deletions
Large diffs are not rendered by default.

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php
+5-6Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@ abstract class RedirectableUrlMatcher extends UrlMatcher implements Redirectable
2525
public function match($pathinfo)
2626
{
2727
try {
28-
$parameters = parent::match($pathinfo);
28+
return parent::match($pathinfo);
2929
} catch (ResourceNotFoundException $e) {
30-
if ('/' === substr($pathinfo, -1) || !in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
30+
if (!\in_array($this->context->getMethod(), array('HEAD', 'GET'), true)) {
3131
throw $e;
3232
}
3333

3434
try {
35-
$parameters = parent::match($pathinfo.'/');
35+
$pathinfo = '/' !== $pathinfo[-1] ? $pathinfo.'/' : substr($pathinfo, 0, -1);
36+
$ret = parent::match($pathinfo);
3637

37-
return array_replace($parameters, $this->redirect($pathinfo.'/', isset($parameters['_route']) ? $parameters['_route'] : null));
38+
return $this->redirect($pathinfo, $ret['_route'] ?? null) + $ret;
3839
} catch (ResourceNotFoundException $e2) {
3940
throw $e;
4041
}
4142
}
42-
43-
return $parameters;
4443
}
4544

4645
/**

‎src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher0.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher0.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function match($rawPathinfo)
1919
{
2020
$allow = array();
2121
$pathinfo = rawurldecode($rawPathinfo);
22-
$trimmedPathinfo = rtrim($pathinfo, '/');
2322
$context = $this->context;
2423
$requestMethod = $canonicalMethod = $context->getMethod();
2524

‎src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function match($rawPathinfo)
1919
{
2020
$allow = array();
2121
$pathinfo = rawurldecode($rawPathinfo);
22-
$trimmedPathinfo = rtrim($pathinfo, '/');
2322
$context = $this->context;
2423
$requestMethod = $canonicalMethod = $context->getMethod();
2524
$host = strtolower($context->getHost());

‎src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher10.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher10.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function match($rawPathinfo)
1919
{
2020
$allow = array();
2121
$pathinfo = rawurldecode($rawPathinfo);
22-
$trimmedPathinfo = rtrim($pathinfo, '/');
2322
$context = $this->context;
2423
$requestMethod = $canonicalMethod = $context->getMethod();
2524

‎src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher11.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher11.php
+50-40Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,30 @@ public function __construct(RequestContext $context)
1515
$this->context = $context;
1616
}
1717

18-
public function match($rawPathinfo)
18+
public function match($pathinfo)
19+
{
20+
try {
21+
return $this->doMatch($pathinfo);
22+
} catch (ResourceNotFoundException $e) {
23+
if (!in_array($this->context->getMethod(), array('HEAD', 'GET'), true)) {
24+
throw $e;
25+
}
26+
27+
try {
28+
$pathinfo = '/' !== $pathinfo[-1] ? $pathinfo.'/' : substr($pathinfo, 0, -1);
29+
$ret = $this->doMatch($pathinfo);
30+
31+
return $this->redirect($pathinfo, $ret['_route']) + $ret;
32+
} catch (ResourceNotFoundException $e2) {
33+
throw $e;
34+
}
35+
}
36+
}
37+
38+
private function doMatch($rawPathinfo)
1939
{
2040
$allow = array();
2141
$pathinfo = rawurldecode($rawPathinfo);
22-
$trimmedPathinfo = rtrim($pathinfo, '/');
2342
$context = $this->context;
2443
$requestMethod = $canonicalMethod = $context->getMethod();
2544

@@ -32,30 +51,30 @@ public function match($rawPathinfo)
3251
0 => '{^(?'
3352
.'|/(en|fr)(?'
3453
.'|/admin/post(?'
35-
.'|/?(*:34)'
36-
.'|/new(*:45)'
54+
.'|/(*:33)'
55+
.'|/new(*:44)'
3756
.'|/(\\d+)(?'
38-
.'|(*:61)'
39-
.'|/edit(*:73)'
40-
.'|/delete(*:87)'
57+
.'|(*:60)'
58+
.'|/edit(*:72)'
59+
.'|/delete(*:86)'
4160
.')'
4261
.')'
4362
.'|/blog(?'
44-
.'|/?(*:106)'
45-
.'|/rss\\.xml(*:123)'
63+
.'|/(*:104)'
64+
.'|/rss\\.xml(*:121)'
4665
.'|/p(?'
47-
.'|age/([^/]++)(*:148)'
48-
.'|osts/([^/]++)(*:169)'
66+
.'|age/([^/]++)(*:146)'
67+
.'|osts/([^/]++)(*:167)'
4968
.')'
50-
.'|/comments/(\\d+)/new(*:197)'
51-
.'|/search(*:212)'
69+
.'|/comments/(\\d+)/new(*:195)'
70+
.'|/search(*:210)'
5271
.')'
5372
.'|/log(?'
54-
.'|in(*:230)'
55-
.'|out(*:241)'
73+
.'|in(*:228)'
74+
.'|out(*:239)'
5675
.')'
5776
.')'
58-
.'|/(en|fr)?(*:260)'
77+
.'|/(en|fr)?(*:258)'
5978
.')$}sD',
6079
);
6180

@@ -64,20 +83,20 @@ public function match($rawPathinfo)
6483
switch ($m = (int) $matches['MARK']) {
6584
default:
6685
$routes = array(
67-
34 => array(array('_route' => 'a', '_locale' => 'en'), array('_locale'), null, null, true),
68-
45 => array(array('_route' => 'b', '_locale' => 'en'), array('_locale'), null, null),
69-
61 => array(array('_route' => 'c', '_locale' => 'en'), array('_locale', 'id'), null, null),
70-
73 => array(array('_route' => 'd', '_locale' => 'en'), array('_locale', 'id'), null, null),
71-
87 => array(array('_route' => 'e', '_locale' => 'en'), array('_locale', 'id'), null, null),
72-
106 => array(array('_route' => 'f', '_locale' => 'en'), array('_locale'), null, null, true),
73-
123 => array(array('_route' => 'g', '_locale' => 'en'), array('_locale'), null, null),
74-
148 => array(array('_route' => 'h', '_locale' => 'en'), array('_locale', 'page'), null, null),
75-
169 => array(array('_route' => 'i', '_locale' => 'en'), array('_locale', 'page'), null, null),
76-
197 => array(array('_route' => 'j', '_locale' => 'en'), array('_locale', 'id'), null, null),
77-
212 => array(array('_route' => 'k', '_locale' => 'en'), array('_locale'), null, null),
78-
230 => array(array('_route' => 'l', '_locale' => 'en'), array('_locale'), null, null),
79-
241 => array(array('_route' => 'm', '_locale' => 'en'), array('_locale'), null, null),
80-
260 => array(array('_route' => 'n', '_locale' => 'en'), array('_locale'), null, null),
86+
33 => array(array('_route' => 'a', '_locale' => 'en'), array('_locale'), null, null),
87+
44 => array(array('_route' => 'b', '_locale' => 'en'), array('_locale'), null, null),
88+
60 => array(array('_route' => 'c', '_locale' => 'en'), array('_locale', 'id'), null, null),
89+
72 => array(array('_route' => 'd', '_locale' => 'en'), array('_locale', 'id'), null, null),
90+
86 => array(array('_route' => 'e', '_locale' => 'en'), array('_locale', 'id'), null, null),
91+
104 => array(array('_route' => 'f', '_locale' => 'en'), array('_locale'), null, null),
92+
121 => array(array('_route' => 'g', '_locale' => 'en'), array('_locale'), null, null),
93+
146 => array(array('_route' => 'h', '_locale' => 'en'), array('_locale', 'page'), null, null),
94+
167 => array(array('_route' => 'i', '_locale' => 'en'), array('_locale', 'page'), null, null),
95+
195 => array(array('_route' => 'j', '_locale' => 'en'), array('_locale', 'id'), null, null),
96+
210 => array(array('_route' => 'k', '_locale' => 'en'), array('_locale'), null, null),
97+
228 => array(array('_route' => 'l', '_locale' => 'en'), array('_locale'), null, null),
98+
239 => array(array('_route' => 'm', '_locale' => 'en'), array('_locale'), null, null),
99+
258 => array(array('_route' => 'n', '_locale' => 'en'), array('_locale'), null, null),
81100
);
82101

83102
list($ret, $vars, $requiredMethods, $requiredSchemes) = $routes[$m];
@@ -88,15 +107,6 @@ public function match($rawPathinfo)
88107
}
89108
}
90109

91-
if (empty($routes[$m][4]) || '/' === $pathinfo[-1]) {
92-
// no-op
93-
} elseif ('GET' !== $canonicalMethod) {
94-
$allow['GET'] = 'GET';
95-
break;
96-
} else {
97-
return array_replace($ret, $this->redirect($rawPathinfo.'/', $ret['_route']));
98-
}
99-
100110
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
101111
if ('GET' !== $canonicalMethod) {
102112
$allow['GET'] = 'GET';
@@ -114,7 +124,7 @@ public function match($rawPathinfo)
114124
return $ret;
115125
}
116126

117-
if (260 === $m) {
127+
if (258 === $m) {
118128
break;
119129
}
120130
$regex = substr_replace($regex, 'F', $m - $offset, 1 + strlen($m));

‎src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher12.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher12.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function match($rawPathinfo)
1919
{
2020
$allow = array();
2121
$pathinfo = rawurldecode($rawPathinfo);
22-
$trimmedPathinfo = rtrim($pathinfo, '/');
2322
$context = $this->context;
2423
$requestMethod = $canonicalMethod = $context->getMethod();
2524

‎src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher13.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher13.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function match($rawPathinfo)
1919
{
2020
$allow = array();
2121
$pathinfo = rawurldecode($rawPathinfo);
22-
$trimmedPathinfo = rtrim($pathinfo, '/');
2322
$context = $this->context;
2423
$requestMethod = $canonicalMethod = $context->getMethod();
2524
$host = strtolower($context->getHost());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.