php - 无法让 XDebug 在 Visual Studio Code 中工作

标签 php apache xdebug

我正在尝试让 XDebug 在 Windows 10 上与 Visual Studio Code 一起使用。我的本地计算机上运行着 Apache/PHP 服务器 (Apache/2.4.41 (Win64) PHP/7.3.9RC1)。它工作没有任何问题。我在同一台计算机上运行 Visual Studio Code。我已经在其中安装了 PHP XDebug 1.13。我在 htdocs 目录中有一个小测试脚本。我设置了几个断点并启动调试器并在调试控制台中获取它...

<- launchResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 2,
  command: 'launch',
  success: true }

然后我切换到 Chrome,此时它有一个箭头沙漏,这样我就可以知道某些东西正在尝试工作。我刷新页面,调试器没有在断点处停止。

我已经阅读了很多帖子。这是我的 php.ini 文件中的相关条目...

[XDebug]
xdebug.stopOnEntry = true
xdebug.remote_host = "127.0.0.1"
xdebug.remote_port=9001
xdebug.remote_connect_back=1
xdebug.remote_enable = 1
xdebug.remote_autorestart = 1
zend_extension = "d:/php/ext/php_xdebug-2.7.2-7.3-vc15-x86_64.dll"

这是我当前的 launch.json 文件...

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9001,
            "hostname": "127.0.0.1",
            "log": true,
         },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9001,


        }
    ]
}

我已经尝试过:

  • 关闭防火墙
  • 更改端口号
  • 在 URL 和 php.ini 中使用“localhost”或 127.0.0.1(我确保在对 php.ini 文件进行任何更改后始终重新启动 Apache。)
  • 我逐渐添加了更多设置(您可以在 php.ini 和 launch.json 中看到,但行为没有改变...启动调试器会导致箭头沙漏大约十秒钟。我保留刷新网页,但它只是运行脚本而不停止。

这是我的测试脚本...

<?php
$a = 3;
$b = 19;
$c = $a + $b;
echo "Answer: $c";
?>

显然,我希望调试器在第一个断点处停止(位于“$a = 3;”行上),但事实并非如此。我尝试添加更多断点。浏览器会直接转到答案“答案:22"

另一件事,我尝试输入 回显“1” 在调试控制台底部的提示中得到了这个:

echo "1"
Cannot evaluate code without a connection
-> evaluateRequest
{ command: 'evaluate',
  arguments: { expression: 'echo "1"', context: 'repl' },
  type: 'request',
  seq: 3 }

所以我猜测网络服务器没有连接。我检查了 error.log 文件,但没有错误...没有任何与调试或连接相关的迹象。

注意:我在防火墙上打开了入站和出站端口,并运行 netstat -ano 并验证端口 9001 正在被监听...

 TCP    127.0.0.1:9001         0.0.0.0:0              LISTENING       16144

任何帮助将不胜感激!!

最佳答案

这是我用来在 Windows + Windows Linux 子系统 (Ubuntu) 设置上运行的 launch.json。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,                
            "stopOnEntry": true, // Once working, comment out this line
            "pathMappings": {
                // eg your web files in C:\Path\To\Code
                "/mnt/c/Path/To/Code": "${workspaceFolder}"
            },
            "log": true            
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
} 

我的 xdebug.ini 是:

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1

关于php - 无法让 XDebug 在 Visual Studio Code 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57648334/

相关文章:

PHP SSL 证书指纹

php - 在 MicroApache 中运行 SQLite 3

php - 如何在 docker-compose 文件中动态获取主机 ip?

php - 没有域名的Apache虚拟主机

apache - Htaccess 将 http 或 https 从 domain.com 重定向到 https domain.net

php - 尽管安装了 Xdebug,但调试器未命中

php - x-debug 导致错误 'undefined property' 错误

php - Laravel 模型的多列 "pivot"表查询

javascript - Xdebug 忽略 php 代码中的断点

php - 共享网络驱动器作为 Laravel 5.4 存储