9

在 VS 代码中,您可以使用 php 可执行文件验证 php。但是有没有办法使用安装在 WSL 上的 php 来代替?

4

4 回答 4

8

什么对我有用:

创建 php.bat 文件:

@echo off
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:c:=/mnt/c%
set v_params=%v_params:"=\"%
@bash -c "php %v_params%"

把它放在 C:\WSL-Tools\php.bat

然后,在 VScode 设置中(Ctrl+逗号):

"php.validate.executablePath": "C:\\WSL-Tools\\php.bat"

它有效。

于 2018-08-07T18:15:10.230 回答
3

另一个答案对我也不起作用:经过一些工作,我想出了这两个脚本:

这个被称为php.bat,我把它放在C:\wsl-tools\

@echo OFF
setlocal ENABLEDELAYEDEXPANSION
rem Collect the arguments and replace:
rem  '\' with '/'
rem  'c:' with 'mnt/c'
rem  '"' with '\"'
set v_params=%*
set v_params=%v_params:\=/%
set v_params=%v_params:C:=/mnt/c%
set v_params=%v_params%
set v_params=%v_params:"=\"%

rem Call the windows-php inside WSL.
rem windows-php is just a script which passes the arguments onto
rem the original php executable and converts its output from UNIX
rem syntax to Windows syntax.
C:\Windows\sysnative\bash.exe -l -c "windows-php %v_params%"

这个叫做 windows-php 并且被放置在 WSL 路径中的某个地方(我选择了/usr/local/bin)。

# Pass all the arguments to PHP.
output=$(php "$@")
# Perform UNIX->WINDOWS syntax replacements.
output="${output//$'\n'/$'\r'$'\n'}"
output="${output//\/mnt\/c/C:}"
output="${output//\//\\}"
# Echo corrected output.
echo $output

设置"php.validate.executablePath": "c:\\wsl-tools\\php.bat"对我有用。

注意

您可能想要关注这个问题这个拉取请求,因为看起来这个问题将在下一个版本中得到正式解决。

于 2017-07-24T16:56:29.577 回答
1

这可以通过老式的批处理文件来实现。

php.bat

@echo off
c:\windows\sysnative\bash.exe -c "php %*"`

设置.json

"php.validate.executablePath": "c:\\PATH_TO\\php.bat"
于 2017-06-23T07:34:55.447 回答
0

如果我还不算太晚,这就是使用 ubuntu 20.04 WSL 2 的方法

@echo OFF
setlocal ENABLEDELAYEDEXPANSION

rem Collect the arguments and replace:
rem  '\' with '/'
rem  'c:' with 'mnt/c'
rem  '"' with '\"'
set v_params=%*
IF DEFINED v_params (
    set v_params=%v_params:\=/%
    set v_params=%v_params:C:=/mnt/c%
    set v_params=%v_params%
    set v_params=%v_params:"=\"%
)
rem Call the windows-php inside WSL.
rem windows-php is just a script which passes the arguments onto
rem the original php executable and converts its output from UNIX
rem syntax to Windows syntax.
@wsl.exe -d Ubuntu-20.04 -- php %v_params%

通过将此 php.bat 放入

c:/wsl插件/

你不需要做 WSL 位。

于 2021-12-21T17:21:10.883 回答