Using XDebug in VSCode
Using XDebug in Visual Studio Code is not difficult, but it's easy to miss one detail that prevents the whole thing from working. Here's a step-by-step tutorial:
    Install the "PHP Debug" VSCode extension by Felix Becker
    Go to the debug sidebar by clicking on the icon that looks like this:
    Click on the gear icon to edit the launch.json configuration file
    Make sure that in the "Debug PHP" section, the port is set to "9000"
    Add this section within "Debug PHP":
1
"pathMappings": {
2
"/var/www/vhosts/elentra-1x-me": "${workspaceRoot}"
3
}
Copied!
    You can also tell the debugger not to show errors from any code that is in the vendor folder (i.e. third-party code) by defining an ignore section, also within "Debug PHP":
1
"ignore": [
2
"**/vendor/**/*.php"
3
],
Copied!
    You can change the name of the section from "Debug PHP" to "Debug Elentra ME", which may help if you switch projects
    Save this file
    Open a terminal window in your "elentra-developer" Docker container
    Look at the /etc/php.d/15-xdebug.inifile and check that the xdebug.remote_enable option is set to "true"
That is all the setup you need, so go to VSCode and click the green "run" triangle at the top of the debug sidebar (beside "Debug PHP" or "Debug Elentra ME" if you changed the section name).
The bottom of the VSCode editor should turn orange and a small debug toolbar should appear near the top of the window. On any executable line of PHP code, click to the left of the line numbers and a small red dot should appear. This is a breakpoint, where PHP will stop before executing the line.
I use the XDebug helper extension in Firefox, so I click on the bug icon in my address bar and select "Debug"
Options for the Firefox XDebug helper
Refresh the page, or click on a link, or do anything else that should execute the code where you placed the breakpoint. The VSCode window should bring itself to the front, with your breakpoint line highlighted. Now you can explore the variables in scope, set watches, and navigate through the call stack. Happy debugging!

Sample launch.json file

1
{
2
// Use IntelliSense to learn about possible attributes.
3
// Hover to view descriptions of existing attributes.
4
// For more information, visit: <https://go.microsoft.com/fwlink/?linkid=830387>
5
"version": "0.2.0",
6
"configurations": [
7
{
8
"name": "Debug Elentra ME",
9
"type": "php",
10
"request": "launch",
11
"port": 9000,
12
"pathMappings": {
13
"/var/www/vhosts/elentra-1x-me": "${workspaceRoot}"
14
},
15
"ignore": [
16
"**/vendor/**/*.php"
17
]
18
}
19
]
20
}
Copied!
Last modified 1mo ago
Copy link