You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改VS Code的launch.json实现直接运行当前PHP文件?

启动PHP内置服务器时直接打开当前编辑文件的配置方法

可以通过修改VS Code的launch.json配置,结合内置变量实现需求,具体操作如下:

  1. 修改serverReadyAction节点
    将原配置中的serverReadyAction替换为以下内容:
"serverReadyAction": {
    "action": "openExternally",
    "pattern": "Development Server \\(http://localhost:9003\\) started",
    "uriFormat": "http://localhost:9003/${relativeFile}"
}
  1. 配置说明
  • ${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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 03:47:22