In symfony - newbie ...


The documentation constantly states that:

Set of files within a directory

etc. That is, that a bundle is a folder in which we collect all the bundle files, thereby keeping everything in one place.

If we generate a new bundle, leaving all the default values, we will get the following picture in the src folder:

 β”œβ”€β”€ Acme β”‚  └── TestBundle β”‚  β”œβ”€β”€ AcmeTestBundle.php β”‚  β”œβ”€β”€ Controller β”‚  β”‚  └── DefaultController.php β”‚  β”œβ”€β”€ Resources β”‚  β”‚  β”œβ”€β”€ config β”‚  β”‚  β”‚  └── services.yml β”‚  β”‚  └── views β”‚  β”‚  └── Default β”‚  β”‚  └── index.html.twig β”‚  └── Tests β”‚  └── Controller β”‚  └── DefaultControllerTest.php 

Now compare this picture with the one that is set when you install symfony.

src folder:

 AppBundle β”œβ”€β”€ AppBundle.php └── Controller └── DefaultController.php 

App folder:

 └── Resources └── views β”œβ”€β”€ base.html.twig └── default └── index.html.twig 

Here we see that the Resource folder is in the app folder.

Why is she here and not in AppBundle ?

  • 2
    Templates in app / Resource can be used as common for different bundles. Maybe that's why they decided to add everything there. Although nothing prevents the use of templates β„–1 Bundla from Bundle β„–2. However, in app / Resource, you can override templates within any bundles. If redefinition is not required, then it is rather a matter of taste where to place common patterns. And if the template is not common, then it’s better to keep it inside src / Acme / Bundle / ... - danil

1 answer 1

As written in Symfony Best Practices : Store all your application's templates in app/Resources/views/ directory.

They argue it with the fact that the names of the templates are becoming smaller and it also becomes more convenient for designers to look for them and in general everything lies in one place. In principle, this is just "advice", and you can stick to the classical structure. In our project, we follow the classic layout of the templates and put all the common parts into app/Resources . And while no problems arise.

And I would like to add that in the app / Resources you can override the templates of other bundles, there they belong.

  • one
    You look too narrowly at the problem. The point is not in the paths to the templates, but in the fact that the application as a whole is in essence not a pure bundle. That is why part of the structure of the application moves from AppBundle to the level of the application itself. - Dmitriy Simushev
  • I do not look too narrowly at the problem and I understand that all the bundles created for the project depend on the application and, in principle, they will never leave it. And all their configs can be in the app / config / services.yml folder. But as I said before, this is a recommendation, and when the project starts to grow to 20+ bundles, the most complex logic, you have to sort it out and even templates - AmsTaFFix
  • I accept your answer so that the question does not hang; however, I note that I, unfortunately (or maybe not), switched to Yii2 due to reasons beyond my control and are now incompetent in Symfony. - Roman Grinyov
  • Eh, it is a pity that we switched to YII2, we are working with SF3 in our project and each time it opens up new opportunities for us)) - AmsTaFFix
  • one
    Be sure to go back to symfony, it has a very correct approach to development, and it is very flexible. - AmsTaFFix