使用默认占位符的symfony2路由

app/config/routing_dev.yml:

people:
    resource: "@myPeopleBundle/Resources/config/routing.yml"
    prefix:   /people

myPeopleBundle/Resources/config/routing.yml:

people_homepage:
    pattern:  /{name}
    defaults: { _controller: myPeopleBundle:Default:index, name: Foo }

people_homepage2:
    pattern:  /
    defaults: { _controller: myPeopleBundle:Default:index, name: Bar }

myPeopleBundle:Controller:DefaultController.php:

...
public function indexAction($name) {
    return $this->render('myPeopleBundle:Default:index.html.twig', array('name' => $name));
}
...

myPeopleBundle:Default:index.html.twig:

Hello {{ name }}!

web/app_dev.php/people -> Hello Foo!
web/app_dev.php/people/ -> Hello Bar!

Why is it different? The people_homepage route why not match the second (web/app_dev.php/people/) url?

But if I set the prefix to / I get the same output:
web/app_dev.php -> Hello Foo!
web/app_dev.php/ -> Hello Foo!

If you look appdevUrlMatcher.php you will see something like this:

    // people_homepage
    if (preg_match('#^/people(?:/(?P<name>[^/]++))?$#s', $pathinfo, $matches)) {
        return $this->mergeDefaults(array_replace($matches, array('_route' => 'people_homepage')), array (  '_controller' => 'Acme\\DemoBundle\\Controller\\DefaultController::indexAction',  'name' => 'Foo',));
    }

    // people_homepage2
    if (rtrim($pathinfo, '/') === '/people') {
        if (substr($pathinfo, -1) !== '/') {
            return $this->redirect($pathinfo.'/', 'people_homepage2');
        }

        return array (  '_controller' => 'Acme\\DemoBundle\\Controller\\DefaultController::indexAction',  'name' => 'Bar',  '_route' => 'people_homepage2',);
    }

You can see that that the route /people/ can not have no match with people_homepage, but it will be with /people or /people/test

For what you are doing would make more sense if you change the order of the routes

people_homepage2:
    pattern:  /
    defaults: { _controller: myPeopleBundle:Default:index, name: Bar }

people_homepage:
    pattern:  /{name}
    defaults: { _controller: myPeopleBundle:Default:index, name: Foo }

With this configuration you will have:

/people == /people/ ---> people_homepage2

/people/test ---> people_homepage