Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Symfony web server from PhpStorm

Is it possible for me to run Symfony web-server from the PhpStorm? I know I can run it from the command line:

php app/console server:run --env=dev

I just wonder if there is any way how to configure PhpStorm to do it for me.

like image 318
Jan Tajovsky Avatar asked Aug 05 '17 12:08

Jan Tajovsky


1 Answers

Symfony WebServer Bundle Setup

  • Click Run -> Edit Configurations
  • In the dialog box Click + (add)
  • Select PHP Script from the drop down list
  • In the name field type Symfony Web-Server
  • In the Script field put the path to your bin/console eg: /path/to/symfony/bin/console
  • Optionally uncheck Activate Tool Window to prevent it from popping up when started
  • In the arguments field type server:run
  • Click Ok to save and close the dialog box

Symfony Web-Server Script

This will provide you with a Run Symfony Web-Server and Debug Symfony Web-Server option to select. To debug the running server, you must tell PHPStorm to listen for debug connections prior to running the server script. To configure debugging in your Settings -> Languages & Frameworks -> PHP -> Servers ensure you have 127.0.0.1:8000 created, disable Path Mapping and you have Xdebug set as the debugger. Optionally disable stop at first line in the Debug -> XDebug settings.

PHP Server Settings

Now you can start the server by selecting it in your Run configuration drop down, and clicking the Play button, or by clicking Run -> Symfony Web-Server.

Be sure to tell PHPStorm to Listen for Debug connections first.

Debug bar

Add breakpoints, then launch your browser to a Route affected by the breakpoint(s) and PHPStorm should capture the debug session and break as desired.

debug output

If debugging fails at first, close all of the running PHPStorm services, and try running the Debug Symfony Web-Server (ensuring that PHPStorm is listening for Debug Connections) and then re-launch your browser. For some reason Xdebug may not initialize otherwise, but this behavior is sporadic and hard to reproduce consistently. After it initializes you can run the non-debug web-server while listening is enabled, and it seems to work fine.

Special note, running the Debug Symfony Web-Server will only monitor the bin/console script. This is because the Symfony server php process is forked and the debug session will be left monitoring the parent process. So you must tell PHPStorm to listen for debug connections when debugging the web server.


Symfony CLI setup

The Symfony Web-Server was added to the new Symfony CLI binary. To continue using the Symfony web server as a PHP Script, described above, you must install the symfony/web-server-bundle compatible with your version of Symfony.

The symfony/web-server-bundle is depreciated as of Symfony 4.4 and will be removed in Syfmony 5.0

To set up the Symfony CLI web-server in PHPStorm, you can create an External Tool.

Program: symfony 
Arguments: server:start --no-tls
Working Directory: $ProjectFileDir$

PHPStorm External Tools

You can then run the command from your Tools menu.

PHPStorm symfony server:start

Or you can execute it from a Run/Debug configuration, similarly to the Symfony WebServer Bundle configuration above, that instead executes the External Tool you created.

PHPStorm symfony server:start configuration

When running either of the above, you should get a new Run dialog CLI inside PHPStorm.

PHPStorm Run symfony server:start

As the Symfony CLI web-server runs outside of the PHP script configuration, it does not suffer the random issues with X-Debug and PHPStorm not listening. I was unable to reproduce the issues in my testing.

However, PHPStorm requires that your server host be changed from http://localhost to http://localhost:8000 for the X-Debug path mapping in PHPStorm to work.

If you do not update the host, PHPStorm will prompt you for the configuration when it encounters a break-point, just click Accept for a new server to be automatically added.

PHPStorm Incoming COnnection from X-Debug

Result

Syfmony 5 server X-Debug

like image 177
Will B. Avatar answered Sep 22 '22 08:09

Will B.