vscode/cursor如何使用小皮运行php代码
配置 vscode/cursor
方法一
安装插件
我使用的是 cursor , cursor 和vscode 是一样的, 直接在 cursor 扩展里面安装了 PHP Extension Pack
, 他自带扩展包 (PHP Debug 和 PHP IntelliSense), 当然如果直接安装这两个也可以, php 版本使用了 5.3.29 应该都是都一样的, 最后安装个 code runner
这个插件, 毕竟在 vscode 中运行别的语言也需要这个, php 也不列外
开启php xdebug功能
先开启 php 的php xdebug 功能(方法: 小皮软件中找到软件管理 -> 找到安装的 php 对应版本选择设置 -> 扩展组件 -> 勾选 profiler输出 和 trace输出 -> 确定)
修改php配置文件
(方法: 小皮软件打开设置 -> 选择配置文件 -> 选择对应版本的 php -> 文件最底下有 [Xdebug]
这些对应的内容)
原配置文件
[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php5.3.29nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.auto_trace=On
xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.3.29nts.xdebug.trace
xdebug.profiler_enable=On
xdebug.profiler_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.3.29nts.xdebug.profiler
xdebug.remote_enable=Off
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
修改后 (多添加了 autostart
, 修改了 enable=1
)
[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php5.3.29nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.auto_trace=On
xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.3.29nts.xdebug.trace
xdebug.profiler_enable=On
xdebug.profiler_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.3.29nts.xdebug.profiler
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
验证是否配置成功
注意: 这里记得保存修改的配置文件, 然后重启服务, 还不放心可以在 www
目录下写一个 .php
文件(文件内容: <?php phpinfo(); ?>), 然后试着在浏览器访问下 (localhost:80), 里面搜索 xdebug
然后找到自己更改的两个配置 xdebug.remote_autostart
和 xdebug.remote_enable
是否都为 on
, 说明就没问题了
配置环境变量
环境变量视频里配的只是用户的 path , 一般配置系统的, 我没有配置, 也运行成功了, 当然了, 还是建议配置一下, 以免后面遇到一些问题
配置插件php debug
(方法: 打开设置 -> 扩展 -> php debug -> 在 setting.json 中编辑), 打开后会看到很多配置项, 因为还有别的插件也自动配置过了, 不用管, 直接找到 "php.debug.executablePath": "",
然后在这个双引号中填入 php 的安装路径
注意: 视频中直接删除了 "php.debug.executablePath": "",
, 然后配置了 "php.validate.executablePath"
, 完事运行会报错的, 不知道视频是怎么运行成功的, 我后面在这个位置找到了毛病, "php.debug.executablePath": "",
用于指定哪个 php 程序运行, "php.validate.executablePath"
用于指定哪个 php 程序进行代码验证和语法检查, 后者可以不强制配置, 前者必须
先复制了路径, 直接粘贴进去是不行的, 如下
"php.debug.executablePath": "D:\phpstudy_pro\Extensions\php\php5.3.29nts",
需要添加成双反斜杠, 最终结果
"php.debug.executablePath": "D:\\phpstudy_pro\\Extensions\\php\\php5.3.29nts",
其实到这这里还是不行的, 还需要再添加可执行程序
"php.debug.executablePath": "D:\\phpstudy_pro\\Extensions\\php\\php5.3.29nts\\php.exe",
这里才算可以
配置launch.json文件
视频里配置了这个文件, 我前面在排查为啥运行不了程序的时候, 发现好像也不需要配置这个一样可以运行, 配置的的话就是打卡那个文件, 然后端口号要和下面的端口 (xdebug.remote_port=9000) 一致
[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php5.3.29nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.auto_trace=On
xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.3.29nts.xdebug.trace
xdebug.profiler_enable=On
xdebug.profiler_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.3.29nts.xdebug.profiler
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
运行
运行的话, 我摸索出来不用从 运行和调试
那边运行, 直接使用 code runner
插件提供的运行按钮 (右上角, 下拉箭头里面有很多选择), 当然了, 这样运行没有快捷键, 可以使用 运行和调试
那边使用快捷键 F5
来运行, 不过还需要改点东西 (在 运行和调试
那个功能里, 有个绿色的运行按钮, 右边的下拉箭头里选择 Debug current script in console
或 Debug current Script in Console
), 运行一次, 后面直接 F5
就可以终端运行了
注意: 此按钮针对 php 一共有四个选项:
第一个会打开浏览器, 也就是小皮启动的 http 服务, 会直接到根目录, 也无法渲染我想要渲染的 .php
文件, 我没有搞清楚为啥, 当然了, 如果没有启动 http 服务, 这个访问了也是不成功的, 而且他需要修改 launch.json
文件, 端口没改之前是 8000
, 可以改成自己小皮创建的网站的端口, 不加端口, 默认就是 80
第二个可以在终端输出 php 执行结果, 这里不需要开启 http 服务
第三个用于监听 debug , 不知道咋玩
第四个和第二个效果一样, 也不需要开启 http 服务
Launch built-in server and debug
Debug current script in console
Listen for Xdebug
Debug current Script in Console
最后 launch.json 的配置文件, 我不使用调试, 所以这里面的我都不用修改
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch built-in server and debug",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-S",
"localhost:8000",
"-t",
"."
],
"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
}
]
}
方法二
这地方我之前是配置过中间件的, 其实自己一键启动也就自动安装了, 别的就没啥了, 这个方法依赖于 http 服务
插件只需要一个 Open PHP/HTML/JS In Browser
然后简单配置一下就可以了, 和前面一样,打开设置 -> 扩展 -> Open PHP/HTML/JS In Browser, 然后配置 selected browser
, 选择自己用于打开渲染代码的浏览器, document root default
用于选择小皮创建的网站的根目录, 我的是 D:\\phpstudy_pro\\WWW\
, 注意反斜号, 我试了下, 斜号, 反斜号都行, 然后就配置好了, 在 php 代码编辑页面右击选择 Open PHP/HTML/JS In Browser
或者 shift+6
就可以在浏览器打开该代码渲染的内容
注意: 运行的话需要一直开着 http 服务
更多推荐
所有评论(0)