Override single route in Symfony2

2019-05-01 10:57发布

问题:

How can I override a single route in Symfony2?

I have a bundle that comes with a bundle_routing.yml file.
In a bundle that extends this parent bundle i also have routing file: routing.xml
Note that the files are named different.

In this routing file I like to override a single parent route.
I tried to simple redeclare it and change the pattern.
But it's not applied.

parent:

MyParentBundle_detailpage:
    pattern:  /detail
    defaults: { _controller: "MyParentBundle:Item:detail" }

child:

<route id="MyParentBundle_detailpage" pattern="/itemDetails">
        <default key="_controller">MyParentBundle:Item:detail</default>
</route>

回答1:

Found the reason myself: Its because of the import order in the main routing file.

app/config/routing.yml

The parent routing must be imported first and the routing of the child bundle must be imported second.

MyParentBundle:
    resource: "@MyParentBundle/Resources/config/bundle_routing.yml"
    prefix:   /
MyChildBundle:
    resource: "@MyChildBundle/Resources/config/routing.xml"
    prefix:   /