0
votes

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?

1
I don't really understand the problem, what would you like to see?Thomas Potaire
I would like to access annotated controllers from MyAdminBundle by paths with /admin prefix and controllers from MyMobileBundle by paths with /mobile prefix.Dmitry Sobolev
Everything seems fine, just make sure the routes' names are not duplicated because you can't have two identical route names. Use the command php app/console router:debug to list all your routes (and route names), by tweaking here and there you will eventually find out the issue.Thomas Potaire
Thomas, do you mean that it is impossible to have /mobile/main and /admin/main paths, for example?Dmitry Sobolev
It is possible to have these two URLs but they shouldn't have the same route name (i.e both routes shouldn't be named "main"). Check my answer below, I've had this problem multiple times already and I am quite sure it will help out.Thomas Potaire

1 Answers

2
votes

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.