如何覆盖核心 Symfony2 类? [英] How can I override Core Symfony2 Classes?

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

问题描述

我想覆盖一个核心的 Symfony2 类.

特别是我想覆盖 vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateReference.php,TemplateReference 这样我就可以改变它找到模板的位置..这可能吗,如果是的话,如何?

解决方案

不要这样做,除非你确切地知道自己在做什么,并且你 100% 确定你不会破坏某些东西......

如果您使用的是 Symfony 2.0 ClassLoader 组件:

$loader->registerNamespaces(array('Symfony' =>大批(__DIR__.'/../src/vendor/symfony/src',__DIR__.'/../vendor/symfony/src',__DIR__.'/../vendor/bundles',),...));

要使用 composer 做到这一点,这应该可以工作,请编辑您的 composer.json 文件:

自动加载":{psr-0":{"": "src/","Symfony": "src/vendor/symfony/src/"}},

这告诉自动加载器,当它试图从 Symfony 命名空间加载一个类时,首先查看你的 src/vendor/symfony/src,然后在vendor/symfony/src 然后在 vendor/bundles ...

然后复制您要在那里编辑的文件:

src/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateReference.php

I want to override a core Symfony2 Class.

Specifically I want to override vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateReference.php, the TemplateReference so I can alter locations where it finds templates.. Is this possible, and if so, how?

解决方案

Do not do this, unless you know exactly what you're doing, and you're 100% sure you won't break something ...

If you're using the Symfony 2.0 ClassLoader component:

$loader->registerNamespaces(array(
    'Symfony' => array(
        __DIR__.'/../src/vendor/symfony/src',
        __DIR__.'/../vendor/symfony/src',
        __DIR__.'/../vendor/bundles',
    ),
    ...
));

To do this with composer, this should work, edit your composer.json file:

"autoload": {
    "psr-0": {
         "": "src/",
         "Symfony": "src/vendor/symfony/src/"
    }
},

This tells the autoloader, that when it tries to load a class from the Symfony namespace, to first look in your src/vendor/symfony/src, then in vendor/symfony/src then in vendor/bundles ...

And then duplicate the file you want to edit there:

src/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateReference.php

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

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