Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow trailing slash for Symfony2 route without params?

Tags:

yaml

symfony

acme_admin_dashboard:
    pattern:  /{_locale}/admin
    defaults: { _controller: AcmeBundle:Admin:dashboard }

I want this route to be accessible at /en/admin and /en/admin/. How would I achieve this?

like image 276
max Avatar asked Mar 20 '12 18:03

max


4 Answers

I like @Kuchengeschmack's answer (https://stackoverflow.com/a/11078348/593957) because it doesn't trigger external redirects.

Here's a yaml version:

acme_admin_dashboard:
    pattern:  /{_locale}/admin{trailingSlash}
    defaults: { _controller: AcmeBundle:Admin:dashboard, trailingSlash : "/" }
    requirements: { trailingSlash : "[/]{0,1}" }
like image 161
walterra Avatar answered Nov 09 '22 01:11

walterra


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...

like image 33
damien Avatar answered Nov 09 '22 00:11

damien


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() {
like image 37
DasBaconfist Avatar answered Nov 09 '22 01:11

DasBaconfist


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/

like image 40
Marek Avatar answered Nov 09 '22 00:11

Marek