Chris
Chris

Reputation: 8109

How to redirect with parameters in routing.yml?

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

Upvotes: 14

Views: 12069

Answers (4)

Eve
Eve

Reputation: 806

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.

Upvotes: 1

iSam4x4
iSam4x4

Reputation: 1

You can redirect the root in .yml like this:

root:
    path: redirected_path
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: destination_path
        permanent: true|false

or

root:
    path: redirected_path
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: destination_route_name
        permanent: true|false

Upvotes: 0

Shakealot
Shakealot

Reputation: 133

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"

Upvotes: 4

Steven Mercatante
Steven Mercatante

Reputation: 25295

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

Upvotes: 16

Related Questions