Learn how to prepare your magento 2 environment to debug properly.

Without debugging enabled, your Magento project is basically a box with secrets. Unless you want to continuously read the log of the project e.g:

tail var/log/exception.log

This is obviously useful, however as a developer that works with a lot of frameworks like Symfony, CakePHP, Laravel, i know how useful it is to see immediately the exception that has been raised in the place where it ocurred. You will need to setup some basic things in your project, so you can simply see errors on the fly.

In this short article, we'll explain how to configure it quickly.

1. Set MAGE_MODE environment variable to developer

The first thing you gotta do is to set the current mode of Magento to the developer mode. This can be done in 2 ways, either directly with the command line. The first you need to do is to know in which mode are you currently set in with:

php bin/magento deploy:mode:show

This will print in the terminal something like:

Current application mode: default. (Note: Environment variables may override this value.)

To set the current mode to the developer mode, where exceptions are thrown directly in the screen and you can see what happens, use the following command:

php bin/magento deploy:mode:set developer

There are 3 modes available that you can set with the CLI:

  • default
  • production
  • developer

That should do the trick, however as mentioned on the command, the environment variables may override the defined behaviour, so to be sure that you are working as a developer in the project, modify the .htaccess file in the root directory of your magento project and define the MAGE_MODE variable to developer:

It is usually in the line #5 of the file and is commented, so you should simply uncomment it and that's it!

# Use the developer mode
SetEnv MAGE_MODE developer

Save the changes and proceed with the next configuration.

2. Enable display errors

As in fact, Magento runs on PHP, you will need to enable the error reporting of PHP. This can be done easily with these 2 lines of code:

ini_set('display_errors', 1);

That usually, exist already on the app/bootstrap.php file and you just need to uncomment the lines of the mentioned instructions. That should be enough to see exceptions, so you can solve them.

Happy coding !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.