在vscode中,当我们安装了插件 PHP Debug(xdebug.php-debug)或者 xdebug.php-pack 后 我们通过内置默认的 php xdebug配置启动php项目后,默认情况下我们在vscode中设置断点是不会生效的,因为我们的内置php服务默认启动时是不会加上xdebug参数的。 这个时候有2中解决方法:
方法一、自定义vscode php内置服务的启动参数,增加xdebug的启动参数
vscode php内置服务配置 .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch built-in server and debug",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.client_host=127.0.0.1",
"-dxdebug.client_port=9003",
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=1",
"-S",
"0.0.0.0:8000",
"-t",
"${cwd}/public",
"${cwd}/public/router.php",
],
"port": 9003,
"serverReadyAction": {
"action": "openExternally"
}
},
{
"name": "Debug current script in console",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"externalConsole": false,
"port": 9003
},
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"serverReadyAction": {
"action": "openExternally"
}
}
]
}
方法二、安装插件 DEVSENSE.phptools-vscode
如果不手动增加上面的配置,就需要安装这个插件,这个插件安装后,我们通过vscode启动PHP内置服务就会自动帮我们增加xdebug的启动参数, 如
Listening to Xdebug on port 0.0.0.0:9003,:::9003 ...
Launching /opt/local/bin/php -dxdebug.client_host=127.0.0.1 -dxdebug.client_port=9003 -dxdebug.mode=debug -dxdebug.start_with_request=1 -S localhost:8000 -t /tp8/server/public /tp8/server/public/router.php ...
PHP Development Server
不过这个插件免费版只能使用基础功能,高级功能是要收费的。