Symfony2 bundle inheritance losing parent bundles routes Symfony2 bundle inheritance losing parent bundles routes php php

Symfony2 bundle inheritance losing parent bundles routes


I found the right solution for this issue. Today I was also trying to override a parent bundle configured with annotations routing and also found that parent routes were ignored if the anotation routing imported the whole bundle ("@SomeBundle/Controller").

After a little debugging I found that the explanation for this is that if you use "@" as prefix for the controller this will pass to the kernel resolver which will return ONLY the child resource if the parent resource has been overridden. So the solution is to provide the full path of the bundle, considering that the kernel will try to match the resource from app/Resources so you will have to add a relative directory (../../) before the actual path:

# app/config/routing.yml:some_parent:    resource: "../../src/Application/ParentBundle/Controller"    type: annotation# ChildBundle implements getParent() method to inherit from ParentBundlesome_child:    resource: "@ChildBundle/Controller"    type: annotation

This will work as expected: all parent routes will be imported and will be overridden by all routes specified in the child bundle.


In addition to previous answer, I also had to change the name of the routing.yml of the child bundle (e.g. to routing_child.yml) to get it working. I assume this is because Symfony totally ignores the parent bundle routing file if the name is identical.

EDIT:In many cases it's also practical to import parent bundle routes into the child bundle routing file like this:

# routing_child.yml     _parent:    resource: "@MyParentBundle/Resources/config/routing.yml"


The official documentation says that you shall just copy parent routing file to your child bundle:

The easiest way to "override" a bundle's routing is to never import it at all. Instead of importing a third-party bundle's routing, simply copying that routing file into your application, modify it, and import it instead.

Also, you cannot include parent's bundle routing file using symbolic names "@ParentBundle" because this name is resolved to "@ChildBundle".

If you really want to include parent routes file, then you shall use the absolute path to that file or path relative to current directory, i.e.:

# @YourChildBundle/Resources/routing.ymlYourParentBundle:  resource: "/srv/www/example.com/src/Your/ParentBundle/Resources/routing.yml"

or

# @YourChildBundle/Resources/routing.ymlYourParentBundle:  resource: "../../../../../Your/ParentBundle/Resources/routing.yml"

Another workaround is to symlink your parent routing file into your child bundle and include it with shorter path, i.e.:

cd YourChildBundeln -s ../../../../../Your/ParentBundle/Resources/routing.yml parent_routes.yml

and then

# @YourChildBundle/Resources/routing.ymlYourParentBundle:  resource: "parent_routing.yml"

P.S. I hope they'll find some better and less uglier way to override and extend routing from parent bundle, but now we have to se some of those ugly workarounds.