0

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
});
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Linesofcode
  • 5,327
  • 13
  • 62
  • 116
  • 1
    I would set `xdebug.start_with_request` to `yes`, not `trigger` – matiaslauriti Feb 27 '23 at 23:04
  • @matiaslauriti that indeed works, but it makes my project to be very slow even if I'm not debugging. How can I solve that? do I need to toggle between yes/no in php.ini everytime I'm debugging? – Linesofcode Feb 27 '23 at 23:15
  • 1
    I honestly have no idea. I use xdebug in a daily basis with `yes`, and I just turn off the listener so I do not get anything from xdebug and it goes flying, so no idea about your slowdown – matiaslauriti Feb 27 '23 at 23:43
  • How are you passing the trigger in each case? – apokryfos Feb 28 '23 at 08:35
  • If you are running the page via browser use one of the Xdebug Helper extensions. See for list: https://xdebug.org/docs/step_debug#browser-extensions – zobo Feb 28 '23 at 13:15
  • @apokryfos dunno, I just though the `trigger` option would be detected when turning on the XDebug in VSCode. IThe problem of having the option `yes` instead of `trigger` is that every request is debugged and it makes the project very slow. – Linesofcode Mar 01 '23 at 18:14
  • Try passing the debug trigger manually e.g. navigate to `http://localhost/test-breakpoint?XDEBUG_TRIGGER=1` – apokryfos Mar 01 '23 at 18:47
  • @apokryfos fantastic, it worked great! Post as an answer so I can accept. – Linesofcode Mar 01 '23 at 18:51

2 Answers2

0

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:\Logs\x-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
        }
    ]
}
  • If I turn the `start_with_request` on, it debugs my code all the time even if I'm not using XDebug, which makes the whole project very slow. – Linesofcode Mar 01 '23 at 18:13
0

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.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • 1
    I would suggest to use a browser extension instead, so that you don't have to manipulate the URL: https://xdebug.org/docs/step_debug#browser-extensions – Derick Mar 06 '23 at 13:50