Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

php - how to overwrite symfony2 core FrameworkBundle?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...