如何在routing.yml中重定向参数?

时间:2012-07-11 15:21:17

标签: symfony

在routing.yml中,您可以执行以下操作:

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

会将网址/old-pattern重定向到/new-pattern。但是,如果我有一个参数,那么如何在新路径中转换参数,例如:

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

这不起作用,会从字面上重定向到/new-pattern/{page},因此会从/old-pattern/23重定向到/new-pattern/{page}

4 个答案:

答案 0 :(得分:16)

如果参数名称相同,则参数将自动传递:

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

答案 1 :(得分:4)

似乎Symfony的书中有一个错误

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

正如Arms所说,它是“FrameworkBundle:Redirect:redirect”而不是“FrameworkBundle:Redirect:urlRedirect”

答案 2 :(得分:1)

我之前错了,这对我有用:

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

直接将parameter_name: parameter_value放在route下。

答案 3 :(得分:0)

您可以像这样重定向.yml中的根目录:

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

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