如果您使用的是 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.phpI 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 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 14:01