Symfony – Debug the value of an environment variable

In Symfony you can define some configuration in the .env file. To be sure of the value the program actually receives at runtime, there is the following command php bin/console debug:container –env-var=DATABASE_URL –env=test With this command you can easily see the value Symfony uses php bin/console debug:container –env-var=DATABASE_URL Symfony Container Environment Variables ======================================= // Displaying … Read more

Symfony – Doctrine – How to see where an SQL query is executed

MacBook avec code sur un bureau

When using Doctrine, it is sometimes important to find where an SQL query is executed. Just turn on profiling_collect_backtrace on the connection, in the development configuration (config/packages/dev/doctrine.yaml): doctrine: dbal: connections: default: profiling_collect_backtrace: true Keep it for development only: collecting the call stack on every query is expensive. It relies on profiling, which is on by … Read more

PHP CS Fixer – How to keep your code clean

Code sur écran d'ordinateur

A really useful tool to keep your code clean is PHP Coding Standards Fixer. It allows us to follow the PHP standards defined in PSR-1, PSR-2, etc. It is also possible to configure it to follow the style of your team with a documentation. The recommended way to install it is outside your code, but … Read more

Symfony4 how to create a custom error page 404

Code sur écran d'ordinateur

You have to start by installing the necessary bundle: composer require symfony/twig-pack When done, create the template error404.html.twig templates/ └─ bundles/ └─ TwigBundle/ └─ Exception/ └─ error404.html.twig Example of the error404.html.twig template {% extends ‘base.html.twig’ %} {% block body %} <h1>Page not found</h1> <p> The requested page couldn’t be located. Checkout for any URL misspelling … Read more

Upload multiple files to an entity with Symfony4 and VichUploaderBundle

Code sur écran d'ordinateur

Preparation   To begin with, we need a Product entity as well as a ProductImage entity that will contain the names of the images. To do this, we use the command in our terminal VichUploaderBundle Setup As stated in the Symfony 4 documentation, we will use the VichUploaderBundle Then add the necessary “config/packages/vich_uploader.yaml” configuration file … Read more

Symfony – debug a Twig template with dump()

Code sur écran d'ordinateur

Twig’s {{ dump() }} function is very handy to inspect a variable inside a template. But without the right package installed, the output is unreadable. Here is how to get a clean, collapsible display like this one: Install the DebugBundle The nice output comes from Symfony’s VarDumper component, enabled by the DebugBundle. Install it as … Read more