8000 Add annotations block and fix regex for redirecting URLs with trailin… · symfony/symfony-docs@10e6b30 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10e6b30

Browse files
petkwouterj
authored andcommitted
Add annotations block and fix regex for redirecting URLs with trailing slash
1 parent 1139127 commit 10e6b30

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

cookbook/routing/redirect_trailing_slash.rst

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,38 @@ system, as explained below:
3737

3838
.. configuration-block::
3939

40+
.. code-block:: php-annotations
41+
42+
// src/AppBundle/Controller/RedirectingController.php
43+
namespace AppBundle\Controller;
44+
45+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
46+
use Symfony\Component\HttpFoundation\Request;
47+
48+
class RedirectingController extends Controller
49+
{
50+
/**
51+
* @Route("/{url}", name="remove_trailing_slash",
52+
* requirements={"url" = ".*\/$"}, methods={"GET"})
53+
*/
54+
public function removeTrailingSlashAction(Request $request)
55+
{
56+
$pathInfo = $request->getPathInfo();
57+
$requestUri = $request->getRequestUri();
58+
59+
$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);
60+
61+
return $this->redirect($url, 301);
62+
}
63+
}
64+
4065
.. code-block:: yaml
4166
4267
remove_trailing_slash:
4368
path: /{url}
4469
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
4570
requirements:
46-
url: .*/$
71+
url: .*\/$
4772
methods: [GET]
4873
4974
.. code-block:: xml
@@ -52,7 +77,7 @@ system, as explained below:
5277
<routes xmlns="http://symfony.com/schema/routing">
5378
<route id="remove_trailing_slash" path="/{url}" methods="GET">
5479
<default key="_controller">AppBundle:Redirecting:removeTrailingSlash</default>
55-
<requirement key="url">.*/$</requirement>
80+
<requirement key="url">.*\/$</requirement>
5681
</route>
5782
</routes>
5883
@@ -70,7 +95,7 @@ system, as explained below:
7095
'_controller' => 'AppBundle:Redirecting:removeTrailingSlash',
7196
),
7297
array(
73-
'url' => '.*/$',
98+
'url' => '.*\/$',
7499
),
75100
array(),
76101
'',

0 commit comments

Comments
 (0)
0