Has anyone successfully configured VSCode to debug Laravel-based website? After having followed numerous articles and tutorials, I have made it to the point where I can ask VSCode to "Listen to XDEBUG", but I haven't been able to do normal VS-style debugging where I could just hit F5 to launch current the website in my favorite browser and it would break into VSCode when it hit a breakpoint, just like we do in full Visual Studio or Eclipse.

我在我的机器上正确设置了以下内容:

  • VSCode 1.25.1
  • XAMPP 1.8
  • XDEBUG (configured and working)
  • PHP Debug extension for VSCode

我不确定我需要在我的launch.json中使用什么启动配置.PHP调试扩展附带的两种配置如下所示:

{
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "port": 9000
},
{
    "name": "Launch currently open script",
    "type": "php",
    "request": "launch",
    "program": "${file}",
    "cwd": "${fileDirname}",
    "port": 9000
}           

虽然第一个配置工作正常(我可以在VSCode中以该模式开始调试,然后在浏览器中单独启动我的网站,它会遇到断点),但第二个配置失败.它告诉我它无法定位Controller类(这是一个Laravel框架类).用名称空间限定类名也没有任何好处.

My guess is that this has got something to with how the launch configuration is setup. It tries to launch the active script as an independent unit and thus fails to locate the definition of framework classes located in different files. We have to somehow provide launch the website as a single application.

有没有人成功地做到了这一点,并告诉我我错过了什么?

推荐答案

Finally got it working. Here are the things if anyone else needs it.

  1. 确保Apache服务器上有XDEBUG set up and running

  2. 为您喜欢的浏览器安装调试器扩展.扩展可用于Chrome、Edge和Firefox(可以在VSCode中搜索和安装).

  3. 设置launch.json,使其并行启动两个配置.这是通过所谓的compound configurations来实现的.下面是我发布PHP+XDEBUG和EDGE浏览器的例子:

     {
         "version": "0.2.0",
         "compounds": [
             {
                 "name": "Launch & Debug",
                 "configurations": [ "Launch Program", "Launch localhost" ]
             }
     ],
     "configurations": [
         {
             "type": "php",
             "request": "launch",
             "name": "Launch Program",
             "cwd": "${workspaceRoot}",
             "port": 9000
         },
         {
             "name": "Launch localhost",
                 "type": "edge",
                 "request": "launch",
                 "url": "http://localhost/public",
                 "webRoot": "${workspaceRoot}"
             }
         ]
     }
    
  4. Update the above config according to your local settings such as site address, xdebug port etc.

  5. 按F5,调试会话将启动.浏览器将自动启动,您可以点击断点.

Laravel相关问答推荐

Nuxt 3获取多部分表单数据上传不起作用

AJAX响应中未定义的全名

在GitHub操作中缺少PHPStan和PHPMD输出,但在本地存在

Livewire 基于下拉 Select 自动填充输入框

从主类别模型 laravel 中获取子类别翻译

如何在 Laravel 中填充以后执行的条件

在 laravel 4.2 中,用户 'root'@'localhost' 的 Laravel 访问被拒绝(使用密码:YES)

Laravel 6 Passport 在错误的凭证上返回 400 Bad request

上传时Laravel正在旋转图像

实施 Payum/Laravel 定期付款

Laravel 5.2 中的正则表达式验证

Laravel 路由将变量传递给控制器

Laravel 5.5 在迁移文件中设置整数字段的大小

调用未定义的方法 Maatwebsite\Excel\Excel::load()

当表中不存在列时如何将 paginate() 与 having() 子句一起使用

Composer 致命错误:声明 Fxp... 必须与第 334 行的 ...AbstractAssetsRepository.php 兼容

laravel 5.1 在没有 VM 重启的情况下看不到作业(job)文件的更改

找不到框laravel/homestead

Laravel 5 - 跳过迁移

Laravel Eager加载与显式连接