Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 MySymfonyBundle;

use SymfonyComponentHttpKernelBundleBundle;

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 MySymfonyBundle;

use SymfonyBundleFrameworkBundleRoutingRouter 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
189 views
Welcome To Ask or Share your Answers For Others

1 Answer

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: MySymfonyBundleRouter

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 MySymfonyBundleResourcesconfigservices.yml assuming you are loading the services file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...