acme_admin_dashboard:
pattern: /{_locale}/admin
defaults: { _controller: AcmeBundle:Admin:dashboard }
I want this route to be accessible a
I found a solution to add a trailing slash to a route.
means both link are working www.example.com/route/of/some/page
and www.example.com/route/of/some/page/
. You can do is this way:
if you route looks like
/**
* @Route("/route/of/some/page")
*/
public function pageAction() {
change ist to
/**
* @Route("/route/of/some/page{trailingSlash}", requirements={"trailingSlash" = "[/]{0,1}"}, defaults={"trailingSlash" = "/"})
*/
public function pageAction() {
For new SF versions:
By default, the Symfony Routing component requires that the parameters match the following regex path: [^/]+
. This means that all characters are allowed except /
.
You must explicitly allow /
to be part of your parameter by specifying a more permissive regex path.
YAML:
_hello:
path: /hello/{username}
defaults: { _controller: AppBundle:Demo:hello }
requirements:
username: .+
XML:
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="_hello" path="/hello/{username}">
<default key="_controller">AppBundle:Demo:hello</default>
<requirement key="username">.+</requirement>
</route>
</routes>
PHP:
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add('_hello', new Route('/hello/{username}', array(
'_controller' => 'AppBundle:Demo:hello',
), array(
'username' => '.+',
)));
return $collection;
Annotations:
/**
* @Route("/hello/{username}", name="_hello", requirements={"username"=".+"})
*/
public function helloAction($username)
{
// ...
}
You could also simply use rewrite rule in the .htaccess file:
Say you have defined a route like so:
news:
url: /news
param: { module: news, action: index }
This will be matched by http://something.something/news, but not by http://something.something/news/ You might add an additional route with a trailing slash, but you could also simply use this rewrite rule in the .htaccess file:
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
http://symfony-blog.driebit.nl/2010/07/url-routes-with-or-without-a-trailing-slash/
I hacked the following line into front controller (app.php / app_dev.php)
$_SERVER['REQUEST_URI'] = preg_replace('|/$|', '', $_SERVER['REQUEST_URI'], 1);
before $request = Request::createFromGlobals()
Just type :
/**
* @Route("/route/of/some/page/")
*/
so
www.example.com/route/of/some/page
and
www.example.com/route/of/some/page/
are accepted...
Route:
remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
methods: [GET]
Controller:
// src/AppBundle/Controller/RedirectingController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class RedirectingController extends Controller
{
public function removeTrailingSlashAction(Request $request)
{
$pathInfo = $request->getPathInfo();
$requestUri = $request->getRequestUri();
$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);
return $this->redirect($url, 301);
}
}
Read more: http://symfony.com/doc/current/routing/redirect_trailing_slash.html