在 VSCode 上调试 Laravel 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51685952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging Laravel application on VSCode
提问by dotNET
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 normalVS-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 来调试基于 Laravel 的网站?在阅读了大量文章和教程之后,我已经达到了可以让 VSCode“聆听 XDEBUG”的程度,但是我无法进行正常的VS 风格的调试,我只能按 F5 启动当前我最喜欢的浏览器中的网站,它会在遇到断点时进入 VSCode,就像我们在完整的 Visual Studio 或 Eclipse 中所做的那样。
I have following things correctly setup on my machine:
我在我的机器上正确设置了以下内容:
- VSCode 1.25.1
- XAMPP 1.8
- XDEBUG (configured and working)
- PHP Debug extension for VSCode
- VSCode 1.25.1
- XAMPP 1.8
- XDEBUG(配置和工作)
- VSCode 的 PHP 调试扩展
I'm not sure what launch configuration do I need to use in my launch.json
. The two configurations that come with PHP Debug extension look like this:
我不确定我需要在我的launch.json
. PHP Debug 扩展附带的两个配置如下所示:
{
"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
}
While the first configuration works correctly (I can start debugging in that mode in VSCode, then launch my website separately in the browser and it hits the breakpoints), the second configuration fails. It tells me that it cannot locate Controller
class (which is a Laravel framework class). Qualifying class name with namespace doesn't do any good either.
虽然第一个配置工作正常(我可以在 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.
我的猜测是这与启动配置的设置方式有关。它尝试将活动脚本作为独立单元启动,因此无法定位位于不同文件中的框架类的定义。我们必须以某种方式将网站作为单个应用程序启动。
Has anyone done that successfully and tell me what I'm missing here?
有没有人成功做到这一点并告诉我我在这里错过了什么?
回答by dotNET
Finally got it working. Here are the things if anyone else needs it.
终于让它工作了。如果其他人需要它,这里是一些东西。
- Make sure you have XDEBUG set up and running on your Apache server.
- Install debugger extension for your favorite browser. Extensions are available for Chrome, Edge and FireFox (can be searched and installed from within VSCode).
Set up
launch.json
so that it launches two configs in parallel. This is done through so-called compound configurations. Here is mine that launches PHP + XDEBUG and EDGE browser:{ "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}" } ] }
- Update the above config according to your local settings such as site address, xdebug port etc.
- Press F5 and your debugging session will start. Browser will launch automatically and you'll be able to hit your breakpoints.
- 确保您已在 Apache 服务器上设置并运行 XDEBUG。
- 为您喜欢的浏览器安装调试器扩展。扩展适用于 Chrome、Edge 和 FireFox(可以从 VSCode 中搜索和安装)。
设置
launch.json
以便它并行启动两个配置。这是通过所谓的复合配置完成的。这是我的启动 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}" } ] }
- 根据您的本地设置(例如站点地址、xdebug 端口等)更新上述配置。
- 按 F5,您的调试会话将开始。浏览器将自动启动,您将能够点击断点。