The Easiest Way To Debug A Symfony Application in PhpStorm

Do you know that many PHP developers do not know how to use IDEs to debug their applications ? It’s actually common practice for majority of them to search for errors by outputting variables to a console. This may sound kind of ridiculous to you if you come from .NET or Java like I do since we are used to work with powerful IDEs. Debugging without breakpoints and watchers sounds terrible and primitive :-)

Obviously, there are good reasons behind why PHP developers don’t use IDE debuggers. I am not going to list the reasons here since this is not our topic. However, I can assure you that after working with many junior & senior PHP developers, I can conclude that the number one reason they don’t use a debugger is simply they don’t know how to set it up :-)

Today, I am going to show a very simple way to use Xdebug, PHPStorm and Symfony in order to debug an application. PHPStorm is the best IDE I have used so far and it comes with amazing functionalities. Anyways here we go:

Run/Debug Configuration

In order to run/debug a Symfony application within PHPStorm, we need to create a new configuration under “Run/Debug Configurations”.

The new configuration will be based as a “PHP script”. The file for this script will point to “bin/console”, which is under our code directory and the arguments should be entered as “server:run”. Basically what this does is that anytime you hit run or debug under PHPStorm, it will call Symfony console with arguments “server:run”. So you don’t need to run your application from terminal anymore. It will be running under PHPStorm’s terminal :-)

Xdebug

The configuration part is basically useless without a debugger. Therefore, we will be using Xdebug’s debugging functionality to capture what’s going on in Symfony. In order to do this, go to Languages & Frameworks > PHP > Servers under PHPStorm preferences.

Add a new server here. The host of the new server will be 127.0.0.1 and port will be 8000 and debugger should be set to Xdebug. Don’t use path mappings since this may cause problems with Symfony.

CLI Interpreter

It’s quite possible that your CLI interpreter might be completely missing or its Xdebug is not configured properly. You need to make sure that your local PHP contains Xdebug configuration.

The “information” button next to PHP executable path will output PHP info. Under PHPinfo you should be able to see the following:

xdebug.remote_autostart=1
xdebug.remote_connect_back=1
xdebug.remote_cookie_expire_time = 3600
xdebug.remote_enable = 0
xdebug.remote_host = localhost
xdebug.remote_port = 9000

xdebug.remote_handler = dbgp

If you don’t have Xdebug values here then you should either put them in a separate INI file or just add it under php.ini. A separate file is usually a recommended way to do this.

Time To Debug

So if everything is setup correctly, then simply hit the run button. This will call “bin/console” and output its contents to PHPStorm’s terminal. The local server should be accessible at http://127.0.0.1:8000.

Let’s add a breakpoint somewhere in your main controller. The breakpoint will be ignored if you don’t tell PHPStorm to start listening for PHP Debug Connections. So click on the following button and make sure PHPStorm is listening.

That’s it! The debugger should now stop at the breakpoint and you should be able to access locals.

Note: You can actually do this with Vagrant or Docker too. I can write an article if enough people are interested. Cheers :-)