How can I override resources for third-party packages in Symfony 4?

I made a new installation of Symfony using Symfony Flex, and the new skeleton belongs to the following Symfony 4 directory structure. Then I am going to redefine some resources , such as templates, translations, etc. From an external package.

I tried to create all of these paths for templates (to run), but nothing works:

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

Where should I put resource files to override third-party package resources?

+5
source share
1 answer

For all versions of Symfony, the path to the resources is %kernel.root_dir%/Resources/ . Since the new 4.0 framework places the Kernel.php directory in src/ , this 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 Resource Override Conventions (since Symfony 3.4)

Twig Templates : Just follow the convention:

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

If you upgrade to Symfony 3.4 and 4.0 and want to use the previous template conventions, configure your own Twig 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. # eg AcmeDemoBundle -> AcmeDemo: templates/bundles/AcmeDemoBundle: AcmeDemo 

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

 translations/bundles/AcmeDemoBundle/messages.en.yml 

Note: /{AcmeDemoBundle}/ part is optional (in this case), because translations are not associated with packages, but with domains. This means that you can redefine translations if they are in the correct domain.

+15
source

Source: https://habr.com/ru/post/1270064/


All Articles