8000 [Routing] Make trailing slashes in urls optional · symfony/routing@c39683b · GitHub
[go: up one dir, main page]

Skip to content

Commit c39683b

Browse files
Seldaekfabpot
authored andcommitted
[Routing] Make trailing slashes in urls optional
1 parent 12ca236 commit c39683b

File tree

1 file changed

+28
-3
lines changed
< 8000 button data-component="IconButton" type="button" data-testid="collapse-file-tree-button" aria-expanded="true" aria-controls="diff_file_tree" data-analytics-opt-out="true" class="prc-Button-ButtonBase-c50BI d-none d-md-flex position-relative fgColor-muted prc-Button-IconButton-szpyj" data-loading="false" data-no-visuals="true" data-size="medium" data-variant="invisible" aria-describedby=":R32plab:-loading-announcement" aria-labelledby="expand-button-file-tree-button">

1 file changed

+28
-3
lines changed

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,49 @@ protected function addMatcher()
6060
$conditions[] = sprintf("isset(\$this->context['method']) && preg_match('#^(%s)$#xi', \$this->context['method'])", $req);
6161
}
6262

63+
$hasTrailingSlash = false;
6364
if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
64-
$conditions[] = sprintf("\$url === '%s'", str_replace('\\', '', $m['url']));
65+
if (substr($m['url'], -1) === '/' && $m['url'] !== '/') {
66+
$conditions[] = sprintf("rtrim(\$url, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
67+
$hasTrailingSlash = true;
68+
} else {
69+
$conditions[] = sprintf("\$url === '%s'", str_replace('\\', '', $m['url']));
70+
}
6571

6672
$matches = 'array()';
6773
} else {
6874
if ($compiledRoute->getStaticPrefix()) {
6975
$conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
7076
}
7177

72-
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $compiledRoute->getRegex());
78+
$regex = $compiledRoute->getRegex();
79+
if ($pos = strpos($regex, '/$')) {
80+
$regex = substr($regex, 0, $pos) . '/?$' . substr($regex, $pos+2);
81+
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $regex);
82+
$hasTrailingSlash = true;
83+
} else {
84+
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $regex);
85+
}
7386

7487
$matches = '$matches';
7588
}
7689

7790
$conditions = implode(' && ', $conditions);
7891

79-
$code[] = sprintf(<<<EOF
92+
$code[] = <<<EOF
8093
if ($conditions) {
94+
EOF;
95+
96+
if ($hasTrailingSlash) {
97+
$code[] = sprintf(<<<EOF
98+
if (substr(\$url, -1) !== '/') {
99+
return array('_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', 'url' => \$this->context['base_url'].\$url.'/', 'permanent' => true, '_route' => '%s');
100+
}
101+
EOF
102+
, $name);
103+
}
104+
105+
$code[] = sprintf(<<<EOF
81106
return array_merge(\$this->mergeDefaults($matches, %s), array('_route' => '%s'));
82107
}
83108

0 commit comments

Comments
 (0)
0