Laravel + Xdebug+ Vscode = Awesome local PHP development.
Hello folks, this is short read for quick Xdebug configuration for debugging Laravel based application. (This also applies to any in general PHP code debug) Also please note that, this is a follow-up article to Configuring and setting up Xdebug for basic PHP development so, if you haven’t checked it, please check it on below link as this article assumes configuration setup from previous article.
Do you Xdebug your PHP code?. Debugging the code is an integral part… | by Rohit P. Shirke | Dec, 2020 | Medium
Now lets get back to the business,
A typical debugging session is based on simple request- response model. The debugger is listening on some port for any upcoming debug request and it will respond accordingly (you hit the breakpoint) when it receive any request.
How the communication actually happens, what is the request and response format is all beyond the scope of our topic hence we will simply skip it for now but if you are still interested, I will recommend to review this link.
So assuming the above preface, we need to configure xdebug to listen for our request, then we need some method to send requests to Xdebug and finally some method to review the response it returned. The later part, sending response and parsing the response will be done by Xdebug and our IDE (Vscode with xdebug extension installed) So we only need to focus on the configuration.
If you have followed the previous article, you are already done with the main configuration part.
Note : For quick reference, please review the below two sections from previous article :
- Configure Xdebug
- Configure Vscode
Debug API’S :
Debugging the API is straight forward, you simply need to add the breakpoint to code and hit the API endpoint to test. The only configuration thing you need to do is make sure you pass the cookie key named “XDEBUG_SESSION” in request header with its value set to the value you have defined in your xdebug.ini/php.ini file.
Please note that the key value you pass should match with the one you defined in config file.
How to test?
- Open vscode with the project you want to debug and add the breakpoint at the required location.
- Hit the F5 button or go run -> Start debugging to start the debugger.
- Hit the API endpoint to debug the code.
Debug Commands / Queue Jobs :
The only configuration you need to debug the command’s is make sure you have the Xdebug trigger key in your environment variable. Just execute the any of the below command based on your platform.
On Linux :
export XDEBUG_CONFIG=”idekey=XDEBUG_VSCODE remote_host=localhost”
On windows :
set XDEBUG_CONFIG=”idekey=XDEBUG_VSCODE remote_host=localhost”
How to test?
- Open the command you want to debug in vscode and add the breakpoint to the required location.
- Hit the F5 button to start debugging.
- On terminal/shell call the command you want to test as you would do normally.
> php artisan testXdebug
If you have followed the steps correctly and did the correct configuration you should see something similar interface for vscode as below for both the debugging sessions.
Note : Any time you make changes to your debug configuration i.e. .ini file you will need to restart the server (XAMP/WAMP/Apache) for changes to take effect.
I hope you enjoyed this read and It will help you!
Thanks and Happy debugging! 😀