如何在Symfony 4中覆盖第三方捆绑包的资源?

8 php symfony symfony4

我使用Symfony Flex进行了新的Symfony安装,新的骨架属于下一个Symfony 4目录结构.接下来,将从外部包中覆盖一些资源,如模板,翻译等.

我试图为模板创建所有这些路径(开始),但没有任何作用:

  • templates/EasyAdminBundle/views/...
  • templates/Resources/EasyAdminBundle/views/...
  • app/Resources/... (只是旧结构的证明)

我应该在哪里放置我的资源文件来覆盖第三方捆绑资源?

yce*_*uto 16

对于所有Symfony版本,资源路径是%kernel.root_dir%/Resources/.由于新的4.0结构放Kernel.phpsrc/目录,那么它是:

# local resources directory
src/Resources/
# still works this path for local templates
src/Resources/views/ 

# override resources for third-party bundles
src/Resources/AcmeDemoBundle/views/ # legacy convention to override templates
src/Resources/AcmeDemoBundle/translations/ # for override both translations and validations files
src/Resources/AcmeDemoBundle/... # etc.
Run Code Online (Sandbox Code Playgroud)

覆盖资源的新约定(自Symfony 3.4起)

Twig模板:只需遵循惯例:

templates/bundles/AcmeDemoBundle/path/to/template.html.twig
Run Code Online (Sandbox Code Playgroud)

如果要升级到Symfony 3.4和4.0并且要使用以前的模板约定,请配置自己的Twig路径:

# app/config/config.yml (3.3)
twig:
    paths:
        # Default templates directory, since 3.4+
        templates: ~

        # Directory convention to override bundle templates, since 3.4+
        # make sure to know the actual Twig namespace for each bundle.
        # e.g. AcmeDemoBundle -> AcmeDemo:
        templates/bundles/AcmeDemoBundle: AcmeDemo
Run Code Online (Sandbox Code Playgroud)

翻译:templates/translations/在项目根目录中的目录类似(默认情况下):

translations/bundles/AcmeDemoBundle/messages.en.yml
Run Code Online (Sandbox Code Playgroud)

注意:/bundles/AcmeDemoBundle/子目录不是必需的,因为转换与bundle无关,而与域有关.这意味着只要它在正确的域中,您就可以覆盖翻译.