v3.3 Debugging

API Platform debugging screencast
Watch the Debugging API Platform screencast

# Xdebug

For development purposes such as debugging tests or remote API requests, Xdebug is shipped by default with the API Platform distribution.

To enable it, run:

XDEBUG_MODE=debug XDEBUG_SESSION=1 docker compose up --wait

# Using Xdebug with PhpStorm

First, create a PHP debug remote server configuration:

  1. In the Settings/Preferences dialog, go to PHP | Servers
  2. Create a new server:
    • Name: api (or whatever you want to use for the variable PHP_IDE_CONFIG)
    • Host: localhost (or the one defined using the SERVER_NAME environment variable)
    • Port: 443
    • Debugger: Xdebug
    • Check Use path mappings
    • Map the local api/ directory to the /app absolute path on the server

You can now use the debugger!

  1. In PhpStorm, open the Run menu and click on Start Listening for PHP Debug Connections

  2. Add the XDEBUG_SESSION=PHPSTORM query parameter to the URL of the page you want to debug or use other available triggers. Alternatively, you can use the Xdebug extension for your preferred web browser.

  3. On the command-line, we might need to tell PhpStorm which path mapping configuration should be used, set the value of the PHP_IDE_CONFIG environment variable to serverName=api, where api is the name of the debug server configured higher.

    Example:

    XDEBUG_SESSION=1 PHP_IDE_CONFIG="serverName=api" php bin/console ...
    

# Using Xdebug With VSCode

If you are using VSCode, use the following launch.json to debug. Note that this configuration includes the path mappings for the Docker image.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "log": true,
            "pathMappings": {
                "/app": "${workspaceFolder}/api"
            },
        },
    ]
}

ℹ️ Note

On Linux, the client_host setting of host.docker.internal may not work. In this case you will need the actual local IP address of your computer.

# Troubleshooting

Inspect the installation with the following command. The requested Xdebug version should be displayed in the output.

$ docker compose exec php \
    php --version

PHP …
    with Xdebug v…, Copyright (c) 2002-2021, by Derick Rethans

You can also help us improve the documentation of this page.

Made with love by

Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.

Learn more

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop