Skip to content

[cookbook] Add annotations block and fix regex #6124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions cookbook/routing/redirect_trailing_slash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,38 @@ system, as explained below:

.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Controller/RedirectingController.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class RedirectingController extends Controller
{
/**
* @Route("/{url}", name="remove_trailing_slash",
* requirements={"url" = ".*\/$"}, methods={"GET"})
*/
public function removeTrailingSlashAction(Request $request)
{
$pathInfo = $request->getPathInfo();
$requestUri = $request->getRequestUri();

$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);

return $this->redirect($url, 301);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the complete code above the configurations so I would reduce this to the bare minimum like this:

.. configuration-block::

    .. code-block:: php+annotations::

        // src/AppBundle/Controller/RedirectingController.php

        /**
         * @Route("/{url}", name="remove_trailing_slash",
         *     requirements={"url" = ".*\/$"}, methods={"GET"})
         */
        public function removeTrailingSlashAction(Request $request)
        {
            // ...
        }

}
}

.. code-block:: yaml

remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
url: .*\/$
methods: [GET]

.. code-block:: xml
Expand All @@ -52,7 +77,7 @@ system, as explained below:
<routes xmlns="http://symfony.com/schema/routing">
<route id="remove_trailing_slash" path="/{url}" methods="GET">
<default key="_controller">AppBundle:Redirecting:removeTrailingSlash</default>
<requirement key="url">.*/$</requirement>
<requirement key="url">.*\/$</requirement>
</route>
</routes>

Expand All @@ -70,7 +95,7 @@ system, as explained below:
'_controller' => 'AppBundle:Redirecting:removeTrailingSlash',
),
array(
'url' => '.*/$',
'url' => '.*\/$',
),
array(),
'',
Expand Down