简体   繁体   中英

How to debug php artisan serve in PHPStorm?

I am using PHPStorm for develop my PHP web pages. All work fine with my Apache Server, XDebug, and a simple web PHP project. No problem.

But, when I try to debug a Laravel 5.1 Web Project using php artisan serve , I can't debug the breakpoints. It's like the php artisan serve use another server...

And on my PHPStorm, I always see:

Waiting for incoming connection with ide key '(randomNumberHere)'

I have configured all in PHPStorm (enabling remote debug, correct port, etc.), and with "normal" PHP projects all works fine.

Can someone tell me if I need to change something?

Thanks!

Debugging using php artisan serve does not work unless you have enabled debugging in ini file.

@Bogdan pointed out the reason. artisan serve will call PHP Built-in Web Server but does not pass on the php command line options (named interpreter options in PHPStorm).

ie if you execute from command line:

$ php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 artisan serve

Then these options given by -d are not passed to called PHP Built-in Web server. You can see the calling of built-in server here .

Workaround in PHPStorm is to create a Run configuration that calls PHP Built-in Web server directly. Instructions:

  1. Open Run -> Edit Configurations...
  2. Create new 'PHP Built-in Web Server'
  3. Set values:
  • Host: localhost
  • Port: 8000
  • Document root: select Laravel's public catalog/directory
  • Check Use route script and select server.php in Laravel projects root directory.
  • Interpreter options: -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1
  1. click OK and run.

Now the PHPStorm will execute same command as php artisan serve does with additional interpreter options. Actually the php artisan serve only purpose is to append the server.php to PHP Built-In Web Server. server.php just emulates Apache's mod_rewrite functionality.

Update: Good reminder from @attila-szeremi: make sure "Start Listening for PHP Debug Connections" is enabled which you manually need to do if you don't run a PhpStorm configuration with "Debug"

I don't use phpstorm, but perhaps the solution that I use for debugging in netbeans will prove useful.

artisan serve uses a different ini file from the one loaded by your web container

Find this by typing

php --ini

On my ubuntu box it's located at

Loaded Configuration File:         /etc/php/7.0/cli/php.ini

Edit the ini for your cli environment and use the same configuration you used to enable it for your web container;

Example...

[Zend]
zend_extension=/usr/lib/php/20151012/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

The only caveat for this, is that as long as you have this configured, it will impact other things that you use php cli for.

Additional note

If you want your debug session to always start automatically (instead of initiating a remote debug via URL request parameter XDEBUG_SESSION_START=name, for example, when debugging CLI stuff), you can set XDEBUG to always start a remote debugging session with this additional configuration;

xdebug.remote_autostart = 1

See https://xdebug.org/docs/all

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Remote Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.

So, after going through Jeffry's Be Awesome in PhpStorm , I had been stuck in a similar situation to the OP (in the chapter regarding Xdebug and Laravel). I was setting up the breakpoints, but PhpStorm was not able to break the execution according to those points. And, to be honest, Josh's answer here somewhat helped me to understand the problem, but was not clear enough for me. So, I went out and found the solution myself by hit and trial method, the final solution of which I want to share. May be it will come in handy for some folks still wondering.

Firstly, as OP said/guessed, php artisan server does use another server. It is PHP's built in web server plus the server.php router file. And the configurations it uses are the CLI configurations. Try php --ini to find all the included configurations.

In my case (Ubuntu 17.10), the Xdebug configuration file being included was from the location /etc/php/7.2/cli/conf.d/20-xdebug.ini . I added the second line to enable remote xdebug and now the file looks like this with total 2 lines:

zend_extension=xdebug.so xdebug.remote_enable=1

Now for configuring PhpStorm:

  1. Run > Edit Configurations and give any name or just leave it as Unnamed
  2. Click ... after the Server: option
  3. Click + and give any name (for example Laravel App )
  4. Set Host as localhost , Port as 8000 and Debugger as Xdebug and leave any other check marks unchecked!
  5. Click Apply / OK
  6. From the drop down list of Server: select the one we just set up.
  7. Set the Start URL: the URL where you want to start debuging (for example \\user\u003c/code> )
  8. Select the browser you prefer.
  9. Click Apply / OK

Please remember that the debugger will only work after the actual web server starts, which is the server that starts with php artisan server command.

Now, start the web server ie. php artisan serve and in PhpStorm click Run > Debug 'your-debug-config' , and everything should work fine!

Part 1 Run > Edit Configurations

choose PHP-BuiltIn Web Server and just set the Host: localhost Port: 8000 // or whichever you are using

在此处输入图片说明

Click OK

Part 2 Install Xdebug helper Chrome extension

在此处输入图片说明

In Chrome browser there should be a bug icon in top right corner Click on it and choose Debug so that the gray bug icon changes the color to green

在此处输入图片说明

Now when you run php artisan serve and set a debug checkpoint

在此处输入图片说明

and click on phone icon to "Start listening for Xdebug connection"

在此处输入图片说明

it should catch your debug checkpoint

Xdebug 3.0 update

Working with XDebug 3.0 is a big easiness now. How I switched on in phpstorm.

My php.ini configuration:

zend_extension = php_xdebug-3.0.0-7.4-vc15-nts-x86_64.dll
xdebug.client_port = 9000
xdebug.mode = debug

Install proper Xdebug extension for your OS: https://xdebug.org/docs/install

Settings in PHPStorm (my version 2020.2.3):

  • Start listening for PHP Debug Connections在此处输入图片说明
  • Managing first connection to server.php after running php artisan serve and first debugging

Browser

Jetbrains has a guide how-to here https://www.jetbrains.com/help/phpstorm/laravel.html#debugging-artisan-commands

So, basically you you need to add php script and point it to artisan file + command在此处输入图片说明

OS: Windows 10 pro

this is working for me:

php 8.0 and xdebug 3+

1.  zend_extension="c:\phplts\ext\php_xdebug.dll"
2.  xdebug.mode=debug
3.  xdebug.start_with_request=yes
4.  xdebug.idekey=PHPSTORM
5.  xdebug.client_host=127.0.0.1
6.  xdebug.client_port=9003
7.  xdebug.remote_handler=dbgp
8.  xdebug.discover_client_host=1

php 7.4 and xdebug 2+

1. zend_extension="c:\phplts\ext\php_xdebug.dll"
2. xdebug.remote_autostart=1
3. xdebug.default_enable=1
4. xdebug.remote_port=9001
5. xdebug.remote_host=127.0.0.1
6. xdebug.remote_connect_back=1
7. xdebug.remote_enable=1
8. xdebug.idekey=PHPSTORM
9. ;xdebug.profiler_enable = 0;
10. ;xdebug.profiler_enable_trigger = 1;
11. ;xdebug.profiler_output_dir="D:\www\php-projects\xdebug" # make sure folder created first and it is writable
12. ;xdebug.profiler_output_name = "cachegrind.out.%t"

PhpStorm

  1. In the Settings/Preferences dialog ( Ctrl + Alt + S ) click PHP under Languages & Frameworks .
  2. in php -> debug change port 9001
  3. in File > Settings > Languages & Frameworks > PHP > Debug > DBGp Proxy
    1. add ide key PHPSTORM
    2. change port 9001
      1. 9003 for xdebug 3+

useful links:

  1. Is it possible to use xdebug on php built-in web server?
  2. Xdebug for remote server not connecting

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