1

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
});
8
  • 1
    I would set xdebug.start_with_request to yes, not trigger Commented Feb 27, 2023 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? Commented Feb 27, 2023 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 Commented Feb 27, 2023 at 23:43
  • How are you passing the trigger in each case? Commented Feb 28, 2023 at 8:35
  • If you are running the page via browser use one of the Xdebug Helper extensions. See for list: xdebug.org/docs/step_debug#browser-extensions Commented Feb 28, 2023 at 13:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest to use a browser extension instead, so that you don't have to manipulate the URL: xdebug.org/docs/step_debug#browser-extensions
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
        }
    ]
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.