30-04-2021



One of the great advantages of an IDE over a text editor is the ability to easily run a debugger. In this lesson you'll learn how to configure PhpStorm to use XDebug. We'll walk through getting XDebug set up, and then how to debug, including setting breakpoints. Note: From the menu bar, PhpStorm Preferences PHP Debug will apply settings to the current project as.

This piece is a guest piece from Isoveran Selwyn Polit!

Phpstorm debug drupal 8
  • Xdebug.remoteenable=true xdebug.profilerenable=0 xdebug.idekey=PHPSTORM xdebug.maxnestinglevel=256 xdebug.remoteautostart=true Can anybody share here some experience? Php drupal phpunit xdebug drupal-8.
  • PhpStorm stops at first line of index.php file you selected. Debugger stopped at first line of index.php file, because we selected 'Break at first line in PHP scripts“ in Run menu. Now it is obvious that debugging connection works, so we can switch this option back off and set some breakpoint in the Drupal 8 code to see some more.

When you use homebrew to install php 7.1 it will cleverly install php-fpm which listens on port 9000. This is the default port for xdebug, so if you want to debug php scripts in a lando container, you will have some challenges.

TL;DR

If you’ve installed php 7.1 with homebrew, it listens on port 9000 so you will need to change the containers php.ini port specification to another port. e.g.

Then tell phpstorm to listen on port 9001

Get a Fresh Drupal 8 instance running with Lando

If you need to, I suggest following the wise instructions of my buddy Benji Fisher to install a fresh instance of Drupal. Make sure it is running in your browser.

Who is Listening?

Only one program can listen on a port at a time. If a program is already listening on the port that you want to use for debugging, it won’t work.

There are several ways to check if a program is listening on port 9000 (the default port for xdebug).

1. In terminal, you can type:

nc -z localhost 9000

if it says:

Something is listening. If you get nothing, then nothing is listening. No need to change any ports.

2. You can run network utility, scan port 9000 to 9003 on 127.0.0.1 (localhost). It looks a little like this. Here port 9002 is being listened to by Phpstorm and port 9000 by php-fpm.

3. A slightly more technical version:

What exactly could this be?

Lando settings

Here is my .lando.yml file. There are some really important parts highlighted in yellow. xdebug: true tells Lando to configure the php in the container to enable xdebug. The php: .lando.php.ini tells Lando to use a custom config file to override some of the php settings.

name: mysite
recipe: drupal8
config:
webroot: docroot
php: 7.2
xdebug: true
config:
php: .lando.php.ini
tooling:
drush:
service: appserver
cmd: drush –root=/app/docroot

Drupal

Some php.ini magic

In order to satisfy the section of the .lando.yml file with this line: php: .lando.php.ini, you will want to create a file called .lando.php.ini in the root of your project. Mine looks like this:

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port=9002

Notice the xdebug.remote_port. This tells the php to send it’s debug info via port 9002 instead of it’s default of port 9000.

Settings in Phpstorm

Look at settings in phpstorm, Languages and frameworks, PHP, Debug and change the port to 9002 to match the setting in the .lando.php.ini file above.

Some Chrome magic

Make sure you have the xdebug helper extension installed in Chrome

Final steps to debugging

Set a breakpoint in the code in phpstorm (or add a line xdebug_break();) Here I’ve added a breakpoint in index.php (the red dot in the middle of the page.)

Tell phpstorm to start listening by clicking the green telephone in the toolbar. (View toolbar to turn on the toolbar.) It is clicked already in the image above.

Now click the green bug in chrome. This causes Chrome to send a debug cookie.

Refresh the page in Chrome (any page from your site) and you should see Phpstorm pop up a scary looking dialog:

Congratulations!

Click Accept and you’ll notice you are debugging:

Troubleshooting

Phpstorm Drupal 8 Download

Phpstorm drupal 8 download

Navigate to /admin/reports/status/php which will tell you the php settings and confirm that:

xdebug.remote_port is set to 9002. If it is set to 9000, Phpstorm will never see any requests and you will be frustrated.

I found it super useful to enable xdebug logging in the .lando.php.ini file like this. I was then able to browse the contents of the xdebug.log file to watch what was happening. The xdebug.log file is created in the root of the project.

The clue was :9000 indicating port 9000 and the fact that it could not connect to client.

Log opened at 2019-02-08 16:31:05
I: Checking remote connect back address.
I: Checking header HTTP_X_FORWARDED_FOR.
I: Remote address found, connecting to 172.20.0.1:9000.
W: Creating socket for 172.20.0.1:9000, poll success, but error: Operation now in progress (29).
E: Could not connect to client. 🙁
Log closed at 2019-02-08 16:31:05

Versions

$ lando version
v3.0.0-rc.7

Mac OSx High Sierra
10.13.6

Phpstorm Drupal 8

PhpStorm 2018.3

References

Check out some of the following useful links for more:

Jetbrains howto article on Phpstorm debugging and Docker
Mike Anello doing some tricky Drupal 8, Xdebug and Lando magic
Tim Jensen gets down and dirty with Xdebug and Lando
The Lando official version of using Drupal 8, Xdebug and Lando (on Linux)

Credits

Thanks to Eugene at Jetbrains tech support for helping me get to the root of this!

Phpstorm Drupal 8

Thanks to the brilliant Benji Fisher at Isovera for his many contributions!

Thanks to Tim Jensen for helping me dig into Xdebug after hearing my pleas for help on the Lando slack channel.

One of the great advantages of an IDE over a text editor is the ability to easily run a debugger. In this lesson you'll learn how to configure PhpStorm to use XDebug. We'll walk through getting XDebug set up, and then how to debug, including setting breakpoints.

Phpstorm Drupal 8 Tutorial

Note: From the menu bar, PhpStorm > Preferences > PHP > Debug will apply settings to the current project as described in the video.

Please note that this series covers PhpStorm versions 6 and 7 only.

Phpstorm Drupal 8 Free

For the latest documentation (including up-to-date videos), see JetBrains documentation.