简体   繁体   中英

How can I debug PHP Laravel on Visual Studio Code without use XAMPP or WAMP server

I'm using PHP 7.3.1 with Laravel 5.7.

How can I proceed to debug step by step without install a external server? Using Xdebug?

Just running with the command:

php artisan serve

All solution then I founded use WAMP.

Setup xdebug extension in php.ini file as per your PHP version.

Check which version of xdebug support your PHP here : https://xdebug.org/wizard.php

Don't forget to change your xdebug extension path zend_extension .

Add xdebug extension into the browser also. For mozila click here.

[xdebug]
zend_extension = "C:\php\ext\php_xdebug-2.7.0-7.2-vc15.dll"
xdebug.remote_autostart = 1
;xdebug.profiler_append = 0
;xdebug.profiler_enable = 0
;xdebug.profiler_enable_trigger = 0
;xdebug.profiler_output_dir = "c:\xampp\tmp"
;xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_enable = 1
;xdebug.remote_handler = "dbgp"
;xdebug.remote_host = "127.0.0.1"
;xdebug.remote_log = "c:\xampp\tmp\xdebug.txt"
;xdebug.remote_port = 9000
;xdebug.trace_output_dir = "c:\xampp\tmp"
;36000 = 10h
;xdebug.remote_cookie_expire_time = 36000
;xdebug.trace_output_dir = "C:\xampp\tmp"

I'd try starting artisan with Xdebug like php -z /path/to/xdebug.so artisan serve .

And then use the PHP debug extension for Visual Studio code, to set breakpoints, inspect variables and all other debugging stuff.

Creating a launch.json file within the .vscode directory of the project with the following contents did the trick for me:

{
     // Use IntelliSense to learn about possible attributes.
     // Hover to view descriptions of existing attributes.
     // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
     "version": "0.2.0",
     "configurations": [
         {
             "name": "Listen for XDebug",
             "type": "php",
             "request": "launch",
             "port": 9000
         }

     ]
}

Finally finished setting up my development environment, where process looks like this:

  • using Homestead/Vagrant/VirtualBox virtual environment and VSCode Debug plugin

  • Edit or after first F5 , .vscode/launch.json will be created with settings like here:

     //file: .vscode/launch.json "version": "0.2.0", "configurations": [ { "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": 9003 }, // ...
  •  # sudo vim /etc/php/8.1/fpm/php.ini zend_extension=xdebug.so xdebug.mode = debug xdebug.discover_client_host = true xdebug.client_port = 9003 xdebug.max_nesting_level = 512 xdebug.idekey = VSCODE xdebug.start_with_request=yes xdebug.client_host=10.0.2.15 # <--- my Homestead/Vagrant/VirtualBox IP address (ip addr show)
  • sudo service php8.1-fpm restart

  • php artisan serve - will deploy/publish/attach php code to php8.1-fpm service

  • 在此处输入图片说明

Notes about development environment:

  • Host OS is Ubuntu Ubuntu 21.10
  • Have installed VirtualBox and Vagrant
  • Homestead cloned and started - vagrant up
  • vagrant ssh-config copy paste to ~/.ssh/config
  • VSCode
    • Remote - SSH
    • Remote - SSH: Editing Configuration Files
  • Using VSCode extension Laravel Extension Pack collection

Additional notes:

  • there are two parts of Laravel/PHP development that could be debugged:
  • php artisan serve - running from shell to deploy/publish/attach php files to php8.1-fpm
    • one way to debug that launching part is to use this command line:
       php \\ -dzend_extension=xdebug.so \\ -dxdebug.mode=debug \\ -dxdebug.discover_client_host=true \\ -dxdebug.client_port=9003 \\ -dxdebug.max_nesting_level=512 \\ -dxdebug.idekey=VSCODE \\ -dxdebug.start_with_request=yes \\ -dxdebug.client_host=10.0.2.15 \\ artisan serve
    • place brake-point somewhere at artisan script in root folder
    • do not need that part at all (just mentioning here for sake of completeness) and could be replaced with php artisan serve
  • service php8.1-fpm status - running in background
    • to debug that part, xdebug.ini file should be configured like here:
       zend_extension=xdebug.so xdebug.mode = debug xdebug.discover_client_host = true xdebug.client_port = 9003 xdebug.max_nesting_level = 512 xdebug.idekey = VSCODE xdebug.start_with_request=yes xdebug.client_host=10.0.2.15
    • always do sudo service php8.1-fpm restart after changing /etc/php/8.1/mods-available/xdebug.ini
    • place brake-point somewhere in SomeCustomController.php file
  • keep running IDE debugger (client) with F5 from ./vscode/launch.json and php parts from above will be able communicate with it over port 9003
  • Homestead is preferred way to develop Laravel/PHP applications (from Laravel docs)
  • Laravel docs:

    There could be more than one php.ini file. In many set-ups there is a different one for the command line (often cli/php.ini ) and the web server (often fpm/php.ini )

Helper commands to check if Xdebug extensions are set:

  • php-fpm8.1 -m - listed
  • php --ini and php -v - not listed and no need for XDebug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM