2

应用程序/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:控制器:DefaultController.php:

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

myPeopleBundle:默认:index.html.twig:

Hello {{ name }}!

web/app_dev.php/people ->你好福!
web/app_dev.php/people/ ->酒吧你好!

为什么不一样?people_homepage路由为什么不匹配第二个 ( ) web/app_dev.php/people/url?

但是,如果我将其设置为prefix/我会得到相同的输出:
web/app_dev.php ->Hello Foo!
web/app_dev.php/ ->你好福!

4

1 回答 1

3

如果你看appdevUrlMatcher.php你会看到这样的东西:

    // 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',);
    }

您可以看到该路线/people/不能与 不匹配people_homepage,但它将与/peopleor/people/test

如果您更改路线的顺序,您正在做的事情会更有意义

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

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

使用此配置,您将拥有:

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

/people/test--->people_homepage

于 2013-03-05T10:46:56.247 回答