如何修改VS Code的launch.json实现直接运行当前PHP文件?
启动PHP内置服务器时直接打开当前编辑文件的配置方法
可以通过修改VS Code的launch.json配置,结合内置变量实现需求,具体操作如下:
- 修改
serverReadyAction节点
将原配置中的serverReadyAction替换为以下内容:
"serverReadyAction": { "action": "openExternally", "pattern": "Development Server \\(http://localhost:9003\\) started", "uriFormat": "http://localhost:9003/${relativeFile}" }
- 配置说明
${relativeFile}是VS Code内置变量,会自动获取当前编辑文件相对于工作区根目录的路径pattern用于匹配PHP内置服务器启动成功的提示信息,保证只有服务器就绪后才打开对应URL- 配置完成后,启动调试时会自动拼接当前文件的URL并在浏览器中打开
完整的launch.json示例:
{ "name": "Launch built-in PHP CLI server", "type": "php", "request": "launch", "runtimeArgs": [ "-S", "localhost:9003", "-t", "./" ], "port": 9003, "serverReadyAction": { "action": "openExternally", "pattern": "Development Server \\(http://localhost:9003\\) started", "uriFormat": "http://localhost:9003/${relativeFile}" } }
注意:如果当前编辑的文件在工作区的子目录中,配置依然有效,比如编辑api/user.php时,会自动打开http://localhost:9003/api/user.php。
内容的提问来源于stack exchange,提问作者Svelte
相关产品推荐
相关产品推荐

