You have created and run your Laravel application. Let’s imagine you have discovered that it functions not the way you expected. For example, it returns a wrong value or crashes with an exception. Seems like you have errors in your code, and it’s time to debug it. I’ll walk you through the steps to setup Xdebug in PHPStorm for Laravel application.
Validate Xdebug and PhpStorm settings
- Open File -> Settings in PhpStorm
- Click on PHP -> Settings (3 dots)
- You should be able to see Xdebug version as below.
If you don’t see Xdebug version information, you may need to install and enable xdebug for your php version.
Create new run configuration
Due to the way
php artisan serve is designed, we cannot use this command (even with additional arguments) to start server in debug mode. We’ll have to create new run configuration.
- Open Run -> Edit Configurations…
- Click “+”, choose ‘PHP Built-In Web Server’
- Set values as below
- Name: Appropriately
- Host: 127.0.0.1
- Port: 8000
- Document root: Select your project’s public/ directory
- Use router script: Select server.php from project directory.
- Command Line – Interpreter options:
-dxdebug.mode=debug -dxdebug.client_port=9000 -dxdebug.client_host=127.0.0.1 -dxdebug.start_with_request=1
Start web server and listen for PHP debug connections
- Click Menu -> Run -> Run…
- Choose the run configuration created in previous step
- Server should be up now
- Enable PHP debug listener in PhpStorm
- Put breakpoints and enjoy debugging.
Make sure “Start Listening for PHP Debug Connections” is enabled while you are debugging. You can click on same button again to disable it. In the screenshot below its enabled (green icon).