首页 文章

Symfony 2多个bundle注释类型路由

提问于
浏览
0

我有一个包含两个捆绑包的Symfony 2.3.1应用程序 . 每个bundle都包含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 回答

  • 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 的路径彼此不重叠 .

相关问题