Question

i'm trying to overwrite Symfony2 Router located at vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

i've followed this tutorial and i've created my own bundle and registered it in AppKernel.php.

<?php
// src/My/SymfonyBundle/MySymfonyBundle.php

namespace My\SymfonyBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MySymfonyBundle extends Bundle
{
    public function getParent()
    {
        // die('test');
        return 'FrameworkBundle';
    }
}

so fat all good. my bundle has been recognized (tested by trying die('test'); in function above)

than i've created my extended version of Symfony2 Router

<?php
// src/My/SymfonBundle/Routing/Router.php

namespace My\SymfonyBundle;

use Symfony\Bundle\FrameworkBundle\Routing\Router as BaseRouter;

die('test');

class Router extends BaseRouter
{}

but it has been ignored. i expected to see my debug test message however my overwritten file is not loaded at all.

i've also seen this question but i that doesn't look clear at all.

how to overwrite Symfony2 core bundle (vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php) or core component (vendor/symfony/symfony/src/Symfony/Component/Routing/Router.php) properly?

Was it helpful?

Solution

You are making it too hard.

If you all you want to do is to plugin your own router class then in app/config/parameters.yml add:

router.class: My\SymfonyBundle\Router

Basically, all of the framework classes can be overridden in this fashion. Take a look at vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resouces/config/routing.xml

Don't use the getParent method. It's not doing what you think it is.

======================================================

Updated to response to a request for information about getParent.

getParent is documented here: http://symfony.com/doc/current/cookbook/bundles/inheritance.html

It lets you override a limited number of files from the parent bundle. It's really designed for tweaking 3rd party bundles. I got very confused trying to use it and tend to avoid it.

======================================================

One final note: I suggested adding router.class to parameters.yml because that was the easiest file to use. But it really should go in the parameters section of My\SymfonyBundle\Resources\config\services.yml assuming you are loading the services file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top