How can I override resources for Third-Party bundles in Symfony 4?

馋奶兔 提交于 2019-12-10 03:17:15

问题


I did a fresh Symfony installation by using Symfony Flex and the new skeleton belong to the next Symfony 4 directory structure. Next, I'm going to override some resources like templates, translations, etc. from an external bundle.

I've tried to create all these paths for templates (to start) but nothing works:

  • templates/EasyAdminBundle/views/...
  • templates/Resources/EasyAdminBundle/views/...
  • app/Resources/... (just a proof from old structure)

Where should I puts my resources files to override third-party bundle resources?


回答1:


For all Symfony versions the resources path is %kernel.root_dir%/Resources/ . As the new 4.0 structure puts the Kernel.php into src/ directory, then it is:

# 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.

New conventions to override resources (since Symfony 3.4)

Twig Templates: Just follow the convention:

templates/bundles/AcmeDemoBundle/path/to/template.html.twig

If you are upgrading to Symfony 3.4 & 4.0 and you want to use the previous templates conventions, configure your own Twig's paths:

# 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

Translations: Similar to templates/ you have the translations/ directory at the root of the project (by default):

translations/bundles/AcmeDemoBundle/messages.en.yml

Note: The /bundles/AcmeDemoBundle/ sub-directory is not mandatory because translations are not related to bundles, but to domains. That means that you can override translations as long as it is in the correct domain.



来源:https://stackoverflow.com/questions/45239350/how-can-i-override-resources-for-third-party-bundles-in-symfony-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!