Symfony 2多个bundle注释类型路由

时间:2013-07-19 21:36:34

标签: symfony routing annotations bundle symfony-2.3

我有一个包含两个捆绑包的Symfony 2.3.1应用程序。每个包都包含Resources / config / routing.yml配置文件:

mobile:
    resource: "@MyMobileBundle/Controller"
    type:     annotation

admin:
    resource: "@MyAdminBundle/Controller"
    type:     annotation

这是app / config / routing.yml:

_mobile:
    resource: "@MyMobileBundle/Resources/config/routing.yml"
    prefix:   /mobile

_admin:
    resource: "@MyAdminBundle/Resources/config/routing.yml"
    prefix:   /admin

app / config / routing_dev.yml包含:

_main:
    resource: routing.yml

问题是每次只有/ admin / ...或/ mobile / ...路径可用。如果app / config / routing.yml中只包含一个路由资源,一切正常。有没有人有这样的问题?以这种方式为不同的包设置前缀是否正确?

1 个答案:

答案 0 :(得分:2)

命令php app/console router:debug是在Symfony2中调试路由的最佳方法。

根据您提供的详细信息,一切似乎都是正确的,并且您说删除其中一个路由前缀会“修复”您的问题。

在数组中可视化您的路线

_mobile: # defines the prefix /mobile
    mobile: # key that defines how you include your controller's route
        main: /mobile/main # "main" is the route name which is duplicated below
_admin: # defines the prefix /admin
    admin: # key that defines how you include your controller's route
        main: /admin/main # this route override the original "main" route

在Symfony2中,路由不是通过添加前缀名称和路由名称来定义的,而是仅通过路由名称来定义。如果您有两条名为main的路由,则Symfony2将只引用一条路径。

在上述情况中,只有/admin/main可以访问,因为它会覆盖/mobile/main

简而言之,您不能拥有两条路线名相同的路线。

因此修复上述示例的最佳解决方案是在路由名称前加上一个键(非常类似于命名空间):

_mobile:
    mobile:
        mobile_main: /mobile/main
_admin:
    admin:
        admin_main: /admin/main

现在您有两条名为admin_mainmobile_main的路线彼此不重叠。

相关问题