0

I have a Symfony 2.3.1 application with two bundles. Each bundle contains Resources/config/routing.yml configuration file:

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

and

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

This is app/config/routing.yml:

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

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

And app/config/routing_dev.yml contains:

_main:
    resource: routing.yml

The problem is that each time only /admin/... or /mobile/... paths are available. If only one routing resource included in app/config/routing.yml everything works fine. Has anybody had such problem? Is it correct to set prefixes for different bundles this way?

4

1 回答 1

2

The command php app/console router:debug is the best way to debug routes in Symfony2.

According to the details you provided everything seems to be correct and you are saying that removing one of the route prefix "fixes" your issue.

Visualizing your routes in an array

_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

In Symfony2 a route isn't defined by the addition of the prefix name and the route name but solely by the route name. If you have two routes named main then Symfony2 will only have reference of one.

In the case above, only /admin/main will be accessible because it overrode /mobile/main.

In short, you can't have two routes with the same route name.

So the best solution to fix the example above is by prefixing the route name with a key (much like namespacing):

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

Now you have two routes named admin_main and mobile_main which don't overlap each other.

于 2013-07-19T22:31:10.953 回答