how to overwrite symfony2 core FrameworkBundle?

☆樱花仙子☆ 提交于 2019-12-13 08:18:24

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/23225839/how-to-overwrite-symfony2-core-frameworkbundle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!