In addition to UnitTest, we still need to use Xdebug for debugging.

So today I’m going to talk about how to use Xdebug in a native Docker environment.

The use here is integrated into VS Code and PHPStorm respectively.

Install Xdebug

It’s based on the Laradock, the divine weapon. Let’s take a look at how to install Xdebug on Laradock.

Install xDebug#

1 – First install xDebug in the Workspace and the PHP-FPM Containers:

a) open the .env file b) search for the WORKSPACE_INSTALL_XDEBUG argument under the Workspace Container c) set it to true d) search for the PHP_FPM_INSTALL_XDEBUG argument under the PHP-FPM Container e) set it to true

2 – Re-build the containers docker-compose build workspace php-fpm

Reference: laradock. IO/documentati…

We modify the corresponding place, and then build, if the following error occurs:

Try adding a domestic source:

RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
Copy the code

Docker-compose up -d nginx: docker-compose up -d nginx: docker-compose up -d nginx: docker-compose up -d nginx: docker-compose up -d nginx

Configure Xdebug

Currently developing IDE, I think the most commonly used are VS Code and PHPStorm. So here’s how to use Xdebug using both ides.

VS Code

Under VS Code, if Xdebug is not installed, simply search for the installation:

After installation, add Xdebug environment configuration:

Json configuration file. We set the port number to be the same as Xdebug under PHP-fpm. Let’s configure pathMappings to associate the project path under docker with the local project path. Details are as follows:

{
    "version": "0.2.0"."configurations": [{"name": "XDebug listening to Laradock"."log": true."type": "php"."request": "launch"."port": 9000."pathMappings": {
                "/var/www/myrss": "${workspaceFolder}",}}, {"name": "Launch currently open script"."type": "php"."request": "launch"."program": "${file}"."cwd": "${fileDirname}"."port": 9000}}]Copy the code

Ok, we start XDebug and print the following to wait for the request:

Let’s write a Demo and set a breakpoint:

Artisan::command('hello'.function (a) {
    $i = 0;
    $i++;

    return "hello".$i;
});
Copy the code

Then start Xdebug and execute the command:

php artisan hello
Copy the code

We can see a lot of input, output, breakpoints and so on:

The variable $I is in an uninitialized state:

We continue at this breakpoint:

PHPStorm

For Mac or Windows 10, the default Docker IP is 10.0.75.1,

Let’s add a Server where:

  • Name: laradock
  • Host: 10.0.75.1
  • Mappings, which is the same as the pathMappings configured by VS Code above

You can then create PHP Remote Debug, where:

  • Server: Associated with the Laradock we built above
  • IDE key: the same as Laradock’s PHp-Fpm

Ok, we can use demo, create breakpoints, run Debug to wait for requests:

PHP artisan hello:

Let’s move on:

conclusion

Using Xdebug to more intuitively understand the dynamic changes of each variable in a method can help us track and troubleshoot code problems. How best to use Xdebug next depends on your actual project and development needs.

reference

  1. Setting up xDebug with PHPUnit using Docker for Mac and PHPStorm intellij-support.jetbrains.com/hc/en-us/co…

  2. Laradock + XDebug + MS Code? No problem medium.com/full-stack-…

  3. The xdebug Laradock using configuration on vscode www.itread01.com/content/152…

  4. How to set VSCode XDebug blog.scottchayaa.com/post/2018/1 on laradock environment…