Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 839
chemitaxis Avatar asked Sep 25 '15 10:09

chemitaxis


People also ask

Is PhpStorm good for laravel?

PhpStorm provides full support of the Laravel Blade template engine. It highlights various Blade syntax constructs, as well as any HTML, JavaScript and CSS code inside the templates. Besides syntax highlighting, PhpStorm provides several other Blade-specific features.

What is php artisan serve command?

The Laravel PHP artisan serve command helps running applications on the PHP development server. As a developer, you can use Laravel artisan serve to develop and test various functions within the application. It also accepts two additional options. You can use the host for changing application's address and port.

How do I run php artisan serve command?

In Laravel, we can run Laravel Development server by typing php artisan serve command on terminal or command line. Make sure your in Laravel's project root directory. If not change your present working directory.


2 Answers

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).

i.e. 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"

like image 189
raigu Avatar answered Sep 24 '22 04:09

raigu


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.

like image 25
Josh Avatar answered Sep 20 '22 04:09

Josh