Good practice in Symfony 2 (from personal experience)

Good day, dear Khabravchians. Today I saw an article on the hub "The official guide to best practices in Symfony" and realized that I have something to fix to add. Here is a list of personal tips and an explanation for them.

Use less annotations

Personally, I love annotations, but with experience I realized that they bring some discomfort. The fact is that the entire configuration cannot be transferred to annotations. There are 2 options left:

  • Maximum in configuration files (e.g. yml);
  • A little bit in files, a little in annotation.

If you choose the second option, then with the growth of the project you get porridge. And your code has more annotations than logic. Excuses like “making routes easier to find” are not accepted. Since if you distribute configuration files correctly, you always know where the routes to certain controllers are located. I am already silent about commands in the console, like route: debug, and a debugger that shows the name of the action and the name of the route.

Bad example (as for me):

Move managers and repositories to separate folders

I’ll give you a bad example right away, as it works on the current project:

All manager classes should be in the Manager folder, Repository repository classes. If this is not done, then with the appearance of 5-10 entities with their managers and repositories, life becomes “more fun."

In templates, always define blocks with javascript and styles

Everything is simple. In each separate page, you can always redefine and load only those javascripts and styles that are needed. I also advise you to leave these blocks empty in the most basic template. And in the bundles when creating the main layout.html.twig to fill them.

Bundle configurations stored in AppBundle / Resources

Even if the project is small, initially get used to storing bundle configurations in AppBundle / Resources /, and not in app / config. In app / only links. This mainly concerns routing and services.

Entity described in the configuration file

Entities are best described in the configuration, for example, in AppBundle / Resources / config / doctrine / Entity.orm.yml, and generated using the doctrine: generate: entities command, which itself will generate entity classes. When entity configs are in files, the classes themselves look cleaner. You will also be sure that there are no errors in them.

For tests, create a separate database

In the parameters.test file, specify the parameters of the test base into which you load fixtures. So you will always be sure that you are testing the same data. Perhaps everything does just that, just on the current project I came across a lack of such practice.

Of course, these are all commonplace things, but there are people who don’t do this and because of this suffer martyrs programmers who write after them.

I would be glad if someone writes their advice in the comments.

Added by :

  • Register form types in DI, and not create via new (otherwise form type extensions do not work) - to0n1
  • Use lazy proxy for dependencies in twig extension (otherwise, a full dependency tree is built for each request) - to0n1
  • If possible, do not use traits with kofig in annotations (there is no way to overlap annotations in a class using a trait) - to0n1

Also popular now: