- I used to debug only with var_dump or echo which was very stupid and exhausted. Laravel's default debug output mitigate the pain greatly, but if u understand how to debug in IDE, everything will be much easier!
- The software versions as below:
- homestead 6.1.0, revised by @Summer
- phpstorm 2018.2
Start a debug session from web browser
Start a debug session from phpstorm
The web browser way (Starting from browser)
-
This way is the most common and very useful for usual development
-
Install Xdebug. It's default installed in homestead.
-
Create a sever. That's very important!
- Open the Preferences, go to the Languages & Frameworks > PHP > Servers, adding a server. Input the Name of server, the Host name and port of the URL u will debug, check Use path mappings, and type the
root
path of Project files on local and remote.
- Open the Preferences, go to the Languages & Frameworks > PHP > Servers, adding a server. Input the Name of server, the Host name and port of the URL u will debug, check Use path mappings, and type the
-
Edit the xdebug configuration file.
- On the vagrant VM side, make sure the
xdebug.ini
contents as below. The homestead contains four php versions, make sure u edit the right one.zend_extension=xdebug.so xdebug.remote_enable = 1 xdebug.remote_connect_back =1 xdebug.remote_port = 9000
- On the vagrant VM side, make sure the
-
Validate debugger configuration on the web server. ( Validate result will tell you how to do. U can see Remote host is configured as ..., don't mind it, as we configure remote_connect_back=1 in the xdebug.ini)
-
Install browser toolbar.
-
Enable listening for PHP Debug Connections.
- All the three steps are too easy to be mentioned.
-
Type the correct Debug port in the Xdebug section of Languages & Frameworks > PHP > Debug, which is the same value with the one in xdebug.ini.
The cli way (Starting from PhpStorm)
-
This way is very useful for debugging single php script file such as artisan.
-
Add the CLI Interpreter. Using the Vagrant way.
- The Path mappings will be filled automatically.
- If u can't find the cli xdebug (That's the default), u could copy the same file from the fpm folder to the cli folder. Don't use the command
sudo phpenmod -s cli xdebug
, because this command will create a symbolic link to the real xdebug.ini file which will lead to the same debug port on cli and web browser debuging. But it seems that two ways cannot work on the same debug port! That's Key!. So u need to edit a bit of the new copied file, just change the xdebug.port to 9001 or others except 9000 ( 9000 seems to be used by web browser debugging by default and unchangeable) as below:zend_extension=xdebug.so xdebug.remote_enable = 1 xdebug.remote_connect_back =1 xdebug.remote_port = 9001
Now, if u want to debug by browser, just keep the default 9000 xdebug port in PhpStorm . If u want to debug by cli, u can just change it to 9001 or other No you set in the xdebug.ini file in the cli folder.
- https://learnku.com/articles/4098/the-first-step-to-becoming-a-senior-php-programmer-debug-xdebug-configuration
- https://xdebug.org/docs/all_settings
- https://www.jetbrains.com/help/phpstorm/debugging-with-phpstorm-ultimate-guide.html#d217222e120
- https://confluence.jetbrains.com/display/PhpStorm/Working+with+Remote+PHP+Interpreters+in+PhpStorm
- https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+CLI+scripts+with+PhpStorm
- https://stackoverflow.com/questions/51619558/debug-cli-php-script-in-phpstorm-cannot-establish-connection-to-xdebug-2-6-0
- If u have installed the
barryvdh/laravel-debugbar
, the debug will break at the first line in thepublic/index.php
which is not convenient. My solution is to uncheck theBreak at first line in PHP scripts
as below: