如何覆盖symfony2核心FrameworkBundle? [英] how to overwrite symfony2 core FrameworkBundle?

查看:91
本文介绍了如何覆盖symfony2核心FrameworkBundle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖位于 vendor / symfony / symfony / src / Symfony / Bundle / FrameworkBundle / Routing / Router.php 中的Symfony2路由器

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

我遵循了此教程,我创建了自己的捆绑软件并将其注册到 AppKernel.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';
    }
}

所以脂肪都很好。我的包已被识别(通过尝试使用 die('test'); 在上面的函数中进行测试)

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

创建了我的Symfony2 Router扩展版本

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
{}

被忽略了。我希望看到调试 test 消息,但是我的覆盖文件根本没有加载。

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.

如何覆盖Symfony2核心捆绑包( vendor / symfony / symfony / src / Symfony / Bundle / FrameworkBundle / Routing / Router.php )还是核心组件( vendor / symfony / symfony / src / Symfony / Component / Routing / Router.php )正确?

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?

推荐答案

您使它变得太难了。

如果您只想插入自己的路由器类,则在app / config / parameters.yml中添加:

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

基本上,所有框架类都可以以此方式覆盖。看看
供应商/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resouces/config/routing.xml

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

不要使用getParent方法。

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

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

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

已更新为对请求的响应有关getParent的信息。

Updated to response to a request for information about getParent.

getParent在此处记录: http://symfony.com/doc/current/cookbook/bundles/inheritance.html

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.

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

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

一个决赛注意:我建议将router.class添加到parameters.yml,因为这是最容易使用的文件。但是,假设您正在加载服务文件,它实际上应该放在My\SymfonyBundle\Resources\config\services.yml的参数部分。

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.

这篇关于如何覆盖symfony2核心FrameworkBundle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆