skip to Main Content

I have read posts like
visual studio code PHP debugging not working and XDebug not working in VScode for php debugging but can’t manage to make this work properly in my Laravel projects.

I’m using XDebug V3 and it works on single php files but not on laravel projects. I use VSCode.

My configuration:

Launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "stopOnEntry": true,
            "log": true,
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000,
            "pathMappings": {
                "C:/xampp/htdocs/myLaravelProject": "${workspaceFolder}"
            }
        }
    ]
}

php.ini

[XDebug]
zend_extension = xdebug
xdebug.mode = debug
xdebug.start_with_request = trigger
xdebug.remote_port = 9000
xdebug.client_port = 9000

Works:
single file index.php

<?php 

$i = 1 + 4;

echo $i; // Breakpoint here works

Doesn’t work: Laravel Project/routes/web.php

<?php 

Route::get('/test-breakpoint', function()
{
   $i = 1 + 4;

   echo $i; // Breakpoint here doesn't work
});

2

Answers


  1. First, Update your php.ini.

    php.ini

    [Xdebug]
    zend_extension=xdebug
    xdebug.mode=debug
    xdebug.start_with_request=yes
    xdebug.client_port=9001
    xdebug.remote_port=9001
    xdebug.log="D:Logsx-debug.log"
    xdebug.log_level=7
    xdebug.idekey=VSCODE
    xdebug.remote_enable=0
    xdebug.profiler_enable=0
    xdebug.remote_autostart=0
    xdebug.client_host="127.0.0.1"
    

    Second, add your Laravel project to a workspace and update launch.json file

    .vscode/launch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Listen for Xdebug",
                "type": "php",
                "request": "launch",
                "pathMappings": {
                    "C:/xampp/htdocs/laravelProject" : "${workspaceFolder}"
                },
                "port": 9001
            }
        ]
    }
    
    Login or Signup to reply.
  2. When using start_with_request=trigger, in order to actually activate debugging, XDebug needs to locate the trigger XDEBUG_TRIGGER. This trigger can be in:

    • $_ENV (though if it’s an environment variable it usually means it’s always on)
    • $_GET
    • $_POST
    • $_COOKIE

    Using Laravel I think the easiest way is to use $_GET which is achieved by simply adding XDEBUG_TRIGGER=1 to the request you want to debug. For example you’d call http://localhost/test-breakpoint?XDEBUG_TRIGGER=1 if you want to run that route with debugging. You can probably also set it in a cookie that only is set on a specific route, but if you choose to do this be aware that Laravel encrypts cookies it sets so you have to use native PHP functions to set this cookie.

    Even if you don’t activate XDebug with a trigger you can still start it using xdebug_break() though this needs to be added to your code which is not always an option.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search