2013-04-01 31 views
0

我想建造URI的使用单个路由模式Symfony2的路由可选参数和格式的路径/图案

1 hello/first.format 
2 hello/first 
3 hello/first/last.format 
4 hello/first/last 

其中需要第一最后格式下面是可选的。

这是我曾尝试:

hello-route: 
    pattern: /hello/{fist_name}/{last_name}.{_format} 
    defaults: { _controller: AcmeHelloBundle:Default:index, last_name:Default, _format:html} 
requirements: 
    _format: html|rss|json 
    first_name: \w+ 
    last_name: \w+ 

但由于它匹配2,3是不正确的,而4而不是1.1将不会失败,但是它会匹配{} FIRST_NAME“第一.format“尽管要求。

我该如何使用基本路由来做到这一点?

回答

1

定义两个途径来完成这个

hello-route-first: 
    pattern: /hello/{fist_name}.{_format} 
    defaults: { _controller: AcmeHelloBundle:Default:index, _format:html} 
requirements: 
    _format: html|rss|json 
    first_name: \w+ 

hello-route-last: 
    pattern: /hello/{fist_name}/{last_name}.{_format} 
    defaults: { _controller: AcmeHelloBundle:Default:index, _format:html} 
requirements: 
    _format: html|rss|json 
    first_name: \w+ 
    last_name: \w+ 

然后使$姓氏参数可选在控制器

function indexAction($first_name, $last_name = "") 
    {