@@ -60,24 +60,49 @@ protected function addMatcher()
60
60
$ conditions [] = sprintf ("isset( \$this->context['method']) && preg_match('#^(%s)$#xi', \$this->context['method']) " , $ req );
61
61
}
62
62
63
+ $ hasTrailingSlash = false ;
63
64
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
+ }
65
71
66
72
$ matches = 'array() ' ;
67
73
} else {
68
74
if ($ compiledRoute ->getStaticPrefix ()) {
69
75
$ conditions [] = sprintf ("0 === strpos( \$url, '%s') " , $ compiledRoute ->getStaticPrefix ());
70
76
}
71
77
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
+ }
73
86
74
87
$ matches = '$matches ' ;
75
88
}
76
89
77
90
$ conditions = implode (' && ' , $ conditions );
78
91
79
- $ code [] = sprintf ( <<<EOF
92
+ $ code [] = <<<EOF
80
93
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
81
106
return array_merge( \$this->mergeDefaults( $ matches, %s), array('_route' => '%s'));
82
107
}
83
108
0 commit comments