14

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

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

这会将 url 重定向/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/23to /new-pattern/{page}

4

4 回答 4

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
于 2012-07-11T16:02:27.153 回答
4

似乎 Symfony 的书中有一个错误

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

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

于 2013-10-17T13:43:36.987 回答
1

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

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

直接放在parameter_name: parameter_value下面route

于 2016-04-25T12:13:09.360 回答
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
于 2015-12-17T15:15:35.307 回答