0

我使用 Symfony 3,我想了解 symfony 是如何选择路由的。

我有 3 条路线,2 条在 routing.yml 中,1 条在注解中。这是我的代码:

应用程序/配置/路由.yml

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

hello:
    path: "hello/{firstName}"
    defaults: {_controller: AppBundle:Default:rotta,firstName: "route1"}

hello_name:
    path: "hello/{firstName}"
    defaults:  {_controller: AppBundle:Default:rotta, firstName: "route2"}

src/AppBundle/Controller/DefaultController.php

/**
 * @Route("hello/{firstName}", name="hello", defaults={"firstName" = "Annotation"})
 */
public function rottaAction($firstName,Request $request)
{
    var_dump($request->get('firstName'));
    exit;
}

我的结果是

string 'route1' (length=6)

有了这个 routing.yml

hello:
    path: "hello/{firstName}"
    defaults: {_controller: AppBundle:Default:rotta,firstName: "route1"}

hello_name:
    path: "hello/{firstName}"
    defaults:  {_controller: AppBundle:Default:rotta, firstName: "route2"}

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

我的结果是

string 'route2' (length=6)

并通过这种组合:

hello:
    path: "hello/{firstName}"
    defaults: {_controller: AppBundle:Default:rotta,firstName: "route1"}

hello_name:
    path: "hello/{firstName}"
    defaults:  {_controller: AppBundle:Default:rotta, firstName: "route2"}

#app:
#    resource: "@AppBundle/Controller/"
#    type:     annotation

我的结果是:

string 'route1' (length=6)
4

0 回答 0