代码之家  ›  专栏  ›  技术社区  ›  Andrei Herford

Symfony 3-覆盖捆绑模板-前置路径不工作

  •  0
  • Andrei Herford  · 技术社区  · 7 年前

    我正在迁移现有的 Symfony 2.8 Symfony 3.4 . 目前,我正在使用bundle继承来扩展 FOSUserBundle 使用我自己的自定义实现。

    这在两种情况下都很好 Symfony 3.4 . 但是,bundle继承已被弃用,希望为将来的更新准备我的项目 Symfony 4

    我知道我可以移动要覆盖的模板 MyUserBundle/Resources/views/... 转到中的应用程序资源文件夹 app/Resources/FOSUserBundle/views/... . 但是,为了更好地重用,我希望将文件保存在我自己的包(或SF4中的src文件夹)中。

    CompilerPass 要将捆绑包中的路径添加到搜索路径,请执行以下操作:

    // src/MyUserBundle/MyUserBundle.php
    class MyUserBundle extends Bundle { 
        //public function getParent() {
        //    return 'FOSUserBundle';
        //}
    
        public function build(ContainerBuilder $container) {
            parent::build($container);  
            $container->addCompilerPass(new OverrideCompilerPass());
        }
    }
    
    // src/MyUserBundle/DependencyInjection/Compiler/OverrideCompilerPass.php
    public function process(ContainerBuilder $container) {
        $path = dirname(__DIR__, 2).'/Resources/views';
        $twigFilesystemLoaderDefinition = $container->findDefinition('twig.loader.native_filesystem');
        $twigFilesystemLoaderDefinition->addMethodCall('prependPath', array($path, 'FOSUser'));
    }
    

    然而,这是行不通的。找不到模板,Symfony使用默认FOS模板。调试器显示以下内容:

      // bin/console debug:twig WITH bundle inheritance
      @FOSUser    src/AppUserBundle/Resources/views/                                             
                  vendor/friendsofsymfony/user-bundle/Resources/views/    
    
      // bin/console debug:twig WITHOUT bundle inheritance
      @FOSUser    vendor/friendsofsymfony/user-bundle/Resources/views/   
    

    如何解决这个问题? 正在使用 app/Resources/...

    0 回复  |  直到 7 年前
    推荐文章