Xdebug is an extension for debugging your PHP code. Magento Cloud Docker provides a separate container to handle Xdebug requests in the Docker environment. Use this container to enable Xdebug and debug PHP code in your Docker environment without affecting your Magento Commerce Cloud project configuration.
The following instructions explain how to configure Xdebug and PhpStorm to debug in your local Docker environment.
If you use Microsoft Windows, take the following steps before continuing:
Install Xdebug helper in Chrome. Search the Chrome web store for Xdebug and find the Xdebug helper extension installation. Phpstorm configuration. Make sure that the Xdebug port in phpstorm is also 9000 (the same as wamp’s Xdebug port): File Setting searches for Xdebug in the search box. Open the configuration in the upper right corner, as.
I use 'Wait for Xdebug connection with ide key' (key=phpstorm) and also configured xdebug.idekey='phpstorm' in php.ini. PhpStorm does not detect any breakpoints. Using 'Open web page in browser. Xdebug helper - Chrome Web Store - Google Chrome, A must have for everyone thats debugging, profiling and tracing PHP code with Xdebug. Configure xDebug with phpStrome. This will cover the following things, Configure xDebug with PHPStrome Configure Xdebug with PHP ini file: -.
- The following video describes how to debug PHP applications using PHP Xdebug extension and PHPStorm.The video describes this on Windows Xampp installation, b.
- Open the debug monitor in phpstorm, that is, the phone button, and then click the green debug crawler button before the code breakpoint. Chrome browser opens the Xdebug page, and phpstorm displays the debug window The figure below shows that your configuration is successful.
- Open your Docker settings.
- Select the Expose daemon on tcp://localhost:2375 without TLS checkbox.
- Wait for the settings to apply.
Enable Xdebug
- To enable Xdebug for your Docker environment, generate the Docker Compose configuration file in developer mode with the
--with-xdebug
option and any other required options, for example.For Linux systems, you must use the--set-docker-host
option to add thehost.docker.internal
entry to the/etc/hosts
file for thefpm_xdebug
container.This command adds the Xdebug configuration to yourdocker-compose.yml
file. - Follow the steps to launch the Docker environment in Developer mode.The default Docker environment configuration sets the following Xdebug configuration variables:
- Change any Xdebug configuration using the
XDEBUG_CONFIG
option. For example, to change the xdebug.remote_port option:On Linux systems, use the following command instead:
To configure PhpStorm to work with Xdebug:
- In your PhpStorm project, open the settings panel.
- Mac OS X—Select File > Preferences.
- Windows/Linux—Select File > Settings.
- In the Settings panel, expand and locate the Languages & Frameworks > PHP > Servers section.
- Click the + to add a
PHP Remote Debug
server configuration. The project name is in grey at the top. - Configure the following settings for the new server configuration:
- Name—Enter the name used for the
serverName
option fromPHP_IDE_CONFIG
value. - Host—Enter
localhost
. - Port—Enter
80
. - Debugger—Select
Xdebug
.
- Name—Enter the name used for the
- Select Use path mappings. In the File/Directory pane, the root of the project for the
serverName
displays. - In the Absolute path on the server column, click and add a value to the
MAGENTO_ROOT
option. The default value is/app
- Change the Xdebug port to 9001 in the Languages & Frameworks > PHP > Debug > Xdebug > Debug Port panel.
- Click Apply.
Use Xdebug
The following steps describe debugging web requests and CLI commands.
Phpstorm Xdebug Docker
To debug web requests:
- In your PhpStorm project, click (Start listening) in the top navigation bar.
- Add breakpoints in the
pub/index.php
file. - Install the debug extension in the browser, and then click Debug to enable.
- In the browser, open the
https://localhost
URL. - When PhpStorm recognizes the Xdebug connection, you can begin debugging web requests.
You can debug any Magento command or PHP script using the following steps.
To debug CLI commands:
- In your PhpStorm project, open the Build, Extension, Deployment > Docker panel, and then click
+
to add a new Docker server and update the following settings:- Name—Enter a name for the server, for example
Docker Cloud
. - Connect to Docker daemon with—
- Windows—Select TCP socket and update Engine Api Url with
tcp://localhost:2375
. - Mac OS X—Select Docker for Mac. [default]
- Windows—Select TCP socket and update Engine Api Url with
- Name—Enter a name for the server, for example
- In the Languages & Frameworks > PHP > Cli Interpreter panel, click […].
- Click [+] to add and configure a new Cli Interpreter from your Docker image. Update the following settings:
- Name—Enter a name for the new interpreter, such as
Magento cloud docker cli
. - Remote—Select
Docker
.- Server—Select
Docker Cloud
from the previous step. - Image name—Select
magento/magento-cloud-docker-php:7.x-cli
.
- Server—Select
- Additional > Debugger extension—
- Windows—Enter
xdebug
. - Mac OS X/Linux—Enter
xdebug.so
.
- Windows—Enter
- Click Refresh to verify that the interpreter and Xdebug extension are configured properly.
- Name—Enter a name for the new interpreter, such as
- Click Save.
- Open the Run/Debug Configuration window and add a new PHP script with the following settings:
- Name—Enter
bin/magento
. - Configuration > File—Select the path to the
bin/magento
file in your local environment.
- Name—Enter
- Add breakpoints in the
bin/magento
file and the debug PHP script created in the previous step.
Using Xdebug Helper
You can install and use the Xdebug Helper Chrome extension to debug your PhP code from the browser.
To use Xdebug Helper with Chrome:
- Install the Xdebug Helper extension from the Chrome store.
- Enable the extension in Chrome as shown in the following figure.
- In Chrome, click in the Chrome toolbar.
- From the Xdebug helper menu, click Options.
- From the IDE Key list, select PhpStorm.
- Click Save.
Supermagnete. Published on 2020-06-21 • Modified on 2020-10-18
In this post, we will see how to do step by step debugging with Xdebug, Symfony and PHPStorm. We will do a basic example where we will stop the execution of the Symfony code just before rendering a template to check the data passed to it. Let's go! ?
» Published in 'A week of Symfony 704' (22-28 June 2020).
Prerequisite
I will assume you have a basic knowledge of PHP, Symfony and that you know how to modify your PHP configuration thanks to the
php.ini
file.Introduction
Why this blog post? Well, because of this tweet:
PHP developers that don't use Xdebug for debugging are amateurs.
— Derick Rethans ? (@derickr) June 20, 2020This is the kind of tweet I don't like, a typical troll, trying to make a generality of something more complex. It brings negativity as It can be interpreted by people not using Xdebug by:
“If you don't use Xdebug, you aren't a real developer.” ?
Even it's not what Derick meant to say, it's what people may understand. There is no smiley. We don't know if the tweet is pure sarcasm or not. I wanted to answer at first. But what about transforming something negative to something positive and useful? ? That's why I decided to write this blog post. ?
Configuration
I use the following configuration, but it should be OK with previous versions of each of these components. Here, I use the Symfony binary to serve my application. If you use another type of setup (Apache, Docker..), you'll probably have to make small adjustments to the following instructions.
- PHP 7.4
- Symfony 5.2
- Xdebug 2.9.6
- PHPStorm 2020.3
Installation
I will assume you have a working PHP/Symfony installation. So first let's install Xdebug, it can be done with PECL:
If not done, activate the Xdebug extension in your
php.ini
file. You can find this file by running:Verify that in this file, the
xdebug.so
(or .dll
) library is loaded. You must see a line like the following (it can also be loaded in an external file like conf.d/ext-xdebug.ini
):If everything is OK, you should now see Xdebug when getting the PHP version:
Or when grepping the module list:
The debug bar also shows if Xdebug is available when you pass over the Symfony version number with your mouse:
Now that Xdebug is activated let's see how to configure it for PHPStorm.
Configuring Xdebug and PHPStorm
Xdebug
First, we must enable the remote option of Xdebug. Add the following parameter in your PHP configuration as we did previously:
We keep the other default parameters to keep the configuration as minimal as possible. So, with this setup, the port used by Xdebug is
9000
and the default IP address is 127.0.0.1
. Check out the xdebug.remote_host
and xdebug.remote_port
parameters in the documentation.PHPStorm
Now, let's check the configuration inside PHPStorm. Open the menu entry: Run > Web Server Debug Validation. You should see this window:
In the first parameter put the full path of your project public directory where is stored the Symfony front controller (generally
public/index.php
with Symfony 5). In the second parameter, put the local URL of your project. Then click on validate. If everything is OK, you should see ✅ like above. You can ignore the error of the last line, it seems to be a known problem, but it won't prevent the debugger from working. The final step is to tell PHPStorm to start listening to Xdebug connections. It must be done with the Run > Start Listening for PHP Debug connections menu entry.Step by step debugging
Now that PHPStorm has validated our setup let's try to add our first breakpoint. Open one of your controllers and click between the line number and the start of the code editor panel of the line you want to stop the execution. A red disc ? appears like this (at line 33 in this example):
Now, open your browser and access a page that calls the action where we put the breakpoint. If it works, PHPStorm gets back as the active window of your OS, and you get the following output:
![Phpstorm xdebug vagrant Phpstorm xdebug vagrant](/uploads/1/1/9/9/119948236/753908346.png)
As you can see, the code window is different from what we use to have. First, after the controller method declaration line, we have the values of the parameters received by the action.
Just below, in the debug panel, we have a Variables section where we can inspect all the local variables available at the breakpoint.
$_locale
is 'en', $goals
is an array with two keys, and lastly, $articleRepository
is the Doctrine repository of the Article
entity. Just below, we see that the line of the breakpoint is highlighted; this is to show that the code has stopped here like expected. Just before this line, after the declaration of the $data
(at the right), we see the value of this new variable. It is empty as we just declared it.Just below, in the debug panel, we have a Variables section where we can inspect all the local variables available at the breakpoint.
This panel is very convenient; we can see all the variables (even the globals) and expand them to check their content. We also find the function parameters (
$_locale, $goals, $articleRepository
). As this controller extends the Symfony AbstractController
, we can notice that it has access to the dependency injection container ($this->container
).Now let's try to advance to the next 'step', to go to the next line. We can use the 'Step over' button (F6 with my setup).
As you can see the highlighted line has changed, it's now line n°34. We can see the value of the
If you don't need the breakpoint for now but want to keep it for later, you can right-click on it and deselect the 'Enabled' option. The red disc appears now as a circle. Refresh the page, and you will notice that the script doesn't stop anymore.
$date
variable just above. This new $date
variable is now part of the 'Variables' panel. We can continue like this until the end of the action to check that the $data
array contains the correct keys and values and can be passed to the Twig template. To continue the execution of the script, click on the 'Resume program' button ⏯️ (F8).If you don't need the breakpoint for now but want to keep it for later, you can right-click on it and deselect the 'Enabled' option. The red disc appears now as a circle. Refresh the page, and you will notice that the script doesn't stop anymore.
The browser extension
We can also install a browser extension (available for Firefox, Chrome, Safari, Opera) to disable/enable the debug on the fly. When disabled, nothing is caught by PHPStorm even there are still some active breakpoints. It is faster than deactivating the breakpoint manually or altogether disable Xdebug in the PHP configuration. It looks like this:
Conclusion
Et voilà! We have a practical step by step debugging workflow using Xdebug! What about telling Derick that we are now professionals PHP developers? ?
About the original tweet, I really liked the answer of Jordi; this is precisely what I think:
I can see a debugger being valuable when code is very complex or unknown, and often use it in JS. In PHP code though I usually am familiar enough with what libs I use and find no benefit to debugging interactively. Like most things, it depends. No need to call people amateurs IMO
— Jordi Boggiano (@seldaek) June 20, 2020If you don't drink Guinness you are an amateur
— Gary Hockin (@GeeH) June 20, 2020PHP developers who don’t use @doctrineproject are amateurs.
— Jonathan H. Wage (@jwage) June 20, 2020Developers that don't use a computer to develop are amateurs
— Gregoire Pineau (@lyrixx) June 21, 2020Phpstorm Xdebug Vagrant
PHP developers that don't write there own frameworks are amateurs
— Simon Bennett (@MrSimonBennett) June 20, 2020PHP developers that write bugs and need to debug are amateurs. https://t.co/NG5YtmdD3k
— Liam Hammett (@LiamHammett) June 20, 2020That's it! I hope you like it. Check out the links below to have additional information related to the post. As always, feedback, likes and retweets are welcome. (see the box below) See you! COil. ?
They gave feedback and helped me to fix errors and typos in this article, many thanks to jmsche. ?
Did you like this post? You can help me back in several ways: (use the Tweet on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and retweet!
- Follow me on Twitter
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win 'Announcer' badges ?.
Phpstorm Setup Xdebug
Thank you for reading! And see you soon on Strangebuzz! ?
[??] New blog post, this is my answer to the tweet: 'PHP developers that don't use #Xdebug for debugging are amateurs.' https://t.co/SPd8UIOrQ8 Proofreading, comments, likes and retweets are welcome! ?Annual goal: 4/6 (66%) #php#strangebuzz#blog#blogging#debug#bug#blogging
— COil #StaySafe ? #OnEstLaTech ✊ (@C0il) June 23, 2020Introducing CW: a cache watcher for Symfony
Configure Xdebug Phpstorm
Adding a custom data collector in the Symfony debug bar