Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect with parameters in routing.yml?

Tags:

symfony

In the routing.yml you can do things like:

redirect_old_url_to_new:
    pattern:   /old-pattern
    defaults:  
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /new-pattern
        permanent: true

Which will redirect the url /old-pattern to /new-pattern. However, if I have a parameter, how can the parameter be translated in the new path, e.g.:

redirect_old_url_to_new:
    pattern:   /old-pattern/{page}
    defaults:  
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /new-pattern/{page}
        permanent: true

This is NOT working and will redirect to /new-pattern/{page} literally and will therefore redirect from /old-pattern/23 to /new-pattern/{page}.

like image 860
Chris Avatar asked Jul 11 '12 15:07

Chris


3 Answers

If the parameter name is identical, then the parameter will be passed automatically:

FirstRoute:
  pattern: /firstroute/{page}
  defaults:
      _controller: Bundle:Controller:action

# SecondRoute will redirect to FirstRoute. 
# ex: /secondroute/1 redirects to /firstroute/1            
SecondRoute:
  pattern: /secondroute/{page}
  defaults:
      _controller: FrameworkBundle:Redirect:redirect
      route: FirstRoute
      permanent: true
like image 90
Steven Mercatante Avatar answered Nov 14 '22 13:11

Steven Mercatante


Seems like there is a mistake in Symfony's Book

root:
    pattern: /
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /app
        permanent: true

As Arms said, it's "FrameworkBundle:Redirect:redirect" and not "FrameworkBundle:Redirect:urlRedirect"

like image 4
Shakealot Avatar answered Nov 14 '22 15:11

Shakealot


I was wrong earlier, this works for me :

redirection:
    path: /exercices.html
    defaults:
      _controller: FrameworkBundle:Redirect:redirect
      route: blog
      slug: url-of-the-post
      permanent: true

Put directly parameter_name: parameter_value under route.

like image 1
Eve Avatar answered Nov 14 '22 13:11

Eve