1. Ensure a debugger session is started for child requests
The way Xdebug and Zend Debugger work is that they make a connection from the PHP interpreter to our IDE. While possible, it's good practice to not do this for every request being made but only for those where debugging is desired. For our frontend, we'll do this from the browser using the PhpStorm bookmarklets or a Browser Debugging Extension.
Both debugging engines can start a debugging session on-demand by passing in a request variable (
XDEBUG_SESSION_START=session_name for Xdebug,
start_debug=1 for Zend Debugger), which is what the bookmarklets and browser extensions do. However, this will only work for the first script we hit. Consider the following two scripts,
When the first script calls into the second script (using
file_get_contents), it starts a separate HTTP connection which is not part of an existing debugging session nor starts a new one. If we want Xdebug or Zend Debugger to start debugging for the called script as well, we'll have to either configure the debugger to start debugging all the time (
xdebug.remote_autostart=1 for Xdebug), or pass the request parameters along.
Let's rewrite the frontend script to do this. In its simplest form, we could check for the Xdebug or Zend Debugger request parameters to be present and just pass them along.
This should start the client debugging session with the same session name as the parent (unless Xdebug was started using cookies, where we fallback to using the default
PHPSTORM session name).
2. Increase the number of simultaneous debugger connections
By default, PhpStorm accepts only one debugger connection at a time. This is a good thing: imagine making extensive use of AJAX and having a debugging session per AJAX call that comes in! For some scenarios, it makes sense to allow more than one connection though, for example for debugging a frontend and backend at the same time.
We can enable support for having more than one debugger connection for our project. In the settings / preferences (Ctrl+Alt+S / CMD+,), expand the Languages & Frameworks | PHP | Debug node and increase the maximum number of connections, for example to 3.
3. Listen for incoming debugger connections
From the toolbar, toggle the “Listen debugger connections” button (or use the Run | Start Listening for PHP Debug Connections menu). This will ensure PhpStorm reacts when a debugging session is started in our application.
4. Start a debugging session
Using the PhpStorm bookmarklets or a Browser Debugging Extension, we can start a debugging session from the browser. If we place a breakpoint in our frontend script, PhpStorm will notify us that there's an incoming debugger connection and pause script execution.
If we step into the
file_get_contents function call, PHP will start a second debugger connection, which in turn opens the debugger in PhpStorm (in a separate tab). We've now effectively stepped from our frontend to the backend.
When the backend script returns, the debugger continues where we left in the frontend script.