How to redirect with parameters in routing.yml?

10,624

Solution 1

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

Solution 2

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"

Solution 3

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.

Share:
10,624

Related videos on Youtube

Chris
Author by

Chris

I love sport, music and Belbo.

Updated on September 14, 2022

Comments

  • Chris
    Chris about 1 year

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

  • DevZer0
    DevZer0 over 8 years
    if the destination route has less parameters than the route being redirected would it still work?
  • David Lin
    David Lin about 8 years
    It will work but extra parameters end up as query parameters to the url.
  • conradkleinespel
    conradkleinespel almost 8 years
    @DavidLin: In Symfony 2.8 (maybe previous versions too), you can ignore extra parameters by setting the ignoreAttributes key in defaults. For instance, ignoreAttributes: [name, age] will ignore the extra name and age parameters, so they will not appear in the URL.