php 单个 symfony 路由中的多个模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11363103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple pattern in single symfony routing
提问by Justin John
How to make multiple pattern in single Symfony routing?
如何在单个 Symfony 路由中制作多个模式?
Normally we have a routing as
通常我们有一个路由
blog:
pattern: /
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
Is it possible to have two routing patterns?
是否可以有两种路由模式?
Something like
就像是
blog:
#Below pattern to match with '/' or '/index'
pattern: {/ , /index}
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
回答by john
Are you using Symfony2? If you are and can use annotations for your routing instead of yml or xml then it's possible to have multiple routes defined along these lines:
你在使用 Symfony2 吗?如果您是并且可以为您的路由使用注释而不是 yml 或 xml,那么可以沿着这些行定义多个路由:
/**
* @Route("/");
* @Route("/home");
*/
Then you don't need to duplicate the action method.
那么你就不需要复制 action 方法了。
回答by René H?hle
The easiest way is to duplicate the block and make 2 routes.
最简单的方法是复制块并制作 2 条路线。
blog:
pattern: /
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
blog_index:
pattern: /index
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
So you have the possibility to use both of them in your path if you need it.
因此,如果需要,您可以在路径中同时使用它们。
Hereyou can see another post how to use regex in your routing. Perhaps you can write a simple regex, which checks whether indexis set.
在这里你可以看到另一篇关于如何在路由中使用正则表达式的帖子。也许您可以编写一个简单的正则表达式来检查是否设置了索引。
Edit:
编辑:
If you work with annotations, which I prefer, then you can write more than one route over your Controller's Action method. Something like this:
如果您使用我更喜欢的注释,那么您可以在控制器的 Action 方法上编写多个路由。像这样的东西:
/**
* @Route("/");
* @Route("/home");
*/
回答by fyrye
When using YAML routes you can also use the node anchorsexpression syntax to reference an existing route definition.
使用 YAML 路由时,您还可以使用节点锚点表达式语法来引用现有路由定义。
&specifies the first occurrence of an anchor, *specifies the anchor to reference, <<tells the Symfony yaml parserto merge the specified node.
&指定锚点的第一次出现,*指定要引用的锚点,<<告诉Symfony yaml 解析器合并指定的节点。
blog: &blog
path: /
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
blog_index:
<<: *blog
path: /index
blog_page:
<<: *blog
path: /blog
Alternatively you can use anchors on a route attribute value.
或者,您可以在路由属性值上使用锚点。
blog:
path: /
defaults: &blog_defaults
_controller: AcmeBlogBundle:Blog:index
page: 1
blog_index:
path: /index
defaults: *blog_defaults
blog_page:
path: /blog
defaults: *blog_defaults
However to prevent poor SEO due to duplicate content, it is recommended to use a redirectinstead.
但是,为了防止由于重复内容而导致 SEO 不佳,建议改用重定向。
blog:
path: /
defaults: { _controller: AcmeBlogBundle:Blog:index, page: 1 }
blog_index:
path: /index
defaults: &blog_redirect
_controller: FrameworkBundle:Redirect:redirect
route: blog
permanent: true
blog_page:
path: /blog
defaults: *blog_redirect
回答by Strabek
Just to add to john's answer:
只是为了补充约翰的答案:
I use it a lot with FOSJsRoutingBundle:
我在 FOSJsRoutingBundle 中经常使用它:
/**
* @Route("/", name="route_name_1", options={"expose"=true})
* @Route("/{id}", name="route_name_2", options={"expose"=true})
* @Method("GET")
* @Template()
*/
This way I have one method and two routes.
这样我就有了一种方法和两条路线。
Just remember to set default $id value:
请记住设置默认的 $id 值:
public function indexAction($id = null)
{
...
}

