简体   繁体   English

如何在 docker 容器中设置 php 时在 vscode 中设置 php 可执行路径 php.validate.executablePath?

[英]How to set php executable path php.validate.executablePath in vscode when php is set inside docker container?

I have a development environment based in docker.我有一个基于 docker 的开发环境。

Everytime that I open VSCode I get this message:每次我打开 VSCode 时都会收到此消息:

Cannot validate since no PHP executable is set.无法验证,因为没有设置 PHP 可执行文件。 Use the setting 'php.validate.executablePath' to configure the PHP executable.使用设置“php.validate.executablePath”来配置 PHP 可执行文件。

Somebody know how to set php.validate.executablePath in this case?有人知道在这种情况下如何设置 php.validate.executablePath 吗?

I have no idea how to do it.我不知道该怎么做。

Here is a screenshot.这是一个屏幕截图。

  • Don't forget to escape \不要忘记逃跑\
  • You don't have to add it to the Path您不必将其添加到路径

设置 php.validate.executablePath 快照

For linux users: if you don't have PHP installed then first download,对于 linux 用户:如果您没有安装 PHP,请先下载,
then in terminal type然后在终端类型

$ whereis php

and it will show path for php executable (It will be either in /usr/bin/php or usr/local/bin/php ) which you can copy from terminal.它将显示 php 可执行文件的路径(它将在/usr/bin/phpusr/local/bin/php中),您可以从终端复制。

In VScode goto settings.json file and paste在 VScode 中转到settings.json文件并粘贴

php.validate.executablePath: /usr/bin/php

In my case using XAMPP is like this就我而言,使用 XAMPP 是这样的

"php.validate.executablePath": "C:/xampp/php/php.exe"

hope that answer your question希望能回答你的问题

Sure, easy.当然,容易。 Just follow what it's says.只要按照它说的去做。

Go to File> Preferences > Settings ... it will open the settings.json file.转到 File> Preferences > Settings ...它将打开 settings.json 文件。

add the following code:添加以下代码:

{"php.validate.executablePath": "Here you put your PHP.exe path"} {"php.validate.executablePath": "这里放你的 PHP.exe 路径"}

You have to know where te PHP.exe file is in your computer, search in the php>bin folder.您必须知道 te PHP.exe 文件在您的计算机中的位置,在 php>bin 文件夹中搜索。

Hope it works for you希望这对你有用

I had the same problem but found the fix here .我有同样的问题,但在这里找到了解决方法。

In Windows:在 Windows 中:

  1. Go To System Properties转到系统属性
  2. Go To Advanced Tab转到高级选项卡
  3. Click "Environment Variables"点击“环境变量”
  4. Select Path选择路径
  5. Add a new path that points to your php 7 executable:添加指向您的 php 7 可执行文件的新路径:

在此处输入图像描述

My case, I'm using Laragon, so you should search where is your php.exe .我的情况,我使用的是 Laragon,所以你应该搜索你的php.exe在哪里。

By VSCode just add.通过VSCode只需添加。

"php.executablePath": "C:\\laragon\\bin\\php\\php-7.2.19-Win32-VC15-x64\\php.exe"

on settings.json .settings.json上。

VSCode Linux Users: VSCode Linux 用户:

Step 1: In your terminal type第 1 步:在您的终端类型中

whereis php

In my case "php: /opt/lampp/bin/php"在我的情况下“php:/opt/lampp/bin/php”

Step 2:第2步:

{
  "php.validate.executablePath": "/opt/lampp/bin/php"
}

I have ubuntu and XAMPP.我有 ubuntu 和 XAMPP。 The following worked for me.以下对我有用。

Press CTRL + P Type settings.json and hit ENTER.按 CTRL + P 键入 settings.json 并按 ENTER。

Add the following line in settings.json在 settings.json 中添加以下行

{
...
"php.validate.executablePath": "/opt/lampp/bin/php",
...
}

Make sure to proper syntax.确保语法正确。

Download PHP, in my case, I downloaded from here: http://windows.php.net/download/下载 PHP,就我而言,我从这里下载:http: //windows.php.net/download/

Copy and paste the files to a location on your computer, and set the path:将文件复制并粘贴到计算机上的某个位置,然后设置路径:

"php.validate.executablePath": "C:/php/php.exe" “php.validate.executablePath”:“C:/php/php.exe”

"php.validate.executablePath": "C:/php/php.exe" wont work due to incorect escaping "php.validate.executablePath": "C:/php/php.exe"由于不正确的转义而无法工作

try this;尝试这个;

"php.validate.executablePath": "C:\\php\\php.exe"

If you get this on windows in VS code click on the blue 'open settings' button.如果您在 VS 代码中的 Windows 上获得此信息,请单击蓝色的“打开设置”按钮。 Near the bottom of the screen this opens there's a link to enter the settings, click this to open the settings.json file.在打开的屏幕底部附近有一个用于输入设置的链接,单击它以打开 settings.json 文件。 Edit the file to look like this:将文件编辑为如下所示:

{
    "php.executablePath" : "C:/php-7.4.2-nts-Win32-vc15-x64/php.exe",
    "php.validate.executablePath" : "C:/php-7.4.2-nts-Win32-vc15-x64/php.exe"
}

where C:/php-7.4.2-nts-Win32-vc15-x64 is the path to your PHP dir其中 C:/php-7.4.2-nts-Win32-vc15-x64 是 PHP 目录的路径

我只是使用了双正斜杠,它就起作用了,如下所示:

"php.validate.executablePath": "C:\\xampp\\php\\php.exe",

I don't know if this helps but I ran into the same problem with the extension PHP Intelephense in Visual Studio Code.我不知道这是否有帮助,但我在 Visual Studio Code 中使用扩展 PHP Intelephense 遇到了同样的问题。 What I did is read the instructions so the extension could work properly.我所做的是阅读说明,以便扩展程序可以正常工作。 So I disabled PHP Language Features by searching "@builtin PHP" in the extensions.所以我通过在扩展中搜索“@builtin PHP”禁用了 PHP 语言功能。 Also, other (3rd party) PHP extensions that provide similar functionality should also be disabled for best results.此外,为了获得最佳效果,也应禁用提供类似功能的其他(第 3 方)PHP 扩展。 Finally, I added glob patterns for non-standard PHP file extensions to the files.associations setting.最后,我在 files.associations 设置中添加了非标准 PHP 文件扩展名的 glob 模式。 For example: "files.associations": { "*.module": "PHP" }例如:“files.associations”:{“*.module”:“PHP”}

Doing this made the error go away.这样做会使错误消失。 I'm new to PHP too, let me know if this is the correct way of solving this problem with PHP Intelephense.我也是 PHP 新手,让我知道这是否是使用 PHP Intelephense 解决此问题的正确方法。

If you're using VScode and have installed Php through MAMP, the default path should be:如果你使用的是 VScode 并且已经通过 MAMP 安装了 Php,那么默认路径应该是:

C:/MAMP/bin/php/php7.4.1/php.exe

/ not \ /不是\

Regarding the docker-specific revision of the question, I found this thread in the VS Code PHP Intellisense git repo issue that explains how to create a wrapper script.关于问题的特定于 docker 的修订,我在 VS Code PHP Intellisense git repo 问题中找到了这个线程,该问题解释了如何创建包装脚本。 It will involve mapping volumes, though, so take that into account.但是,它将涉及映射卷,因此请考虑到这一点。

https://github.com/felixfbecker/vscode-php-intellisense/issues/150#issuecomment-330374124 https://github.com/felixfbecker/vscode-php-intellisense/issues/150#issuecomment-330374124

I also had this error.我也有这个错误。 I have installed Laragon and this addition in setting.json worked for me.我已经安装了 Laragon,setting.json 中的这个添加对我有用。

"php.executablePath":"C:/laragon/bin/php/php-7.2.19-Win32-VC15-x64/php.exe" "php.executablePath":"C:/laragon/bin/php/php-7.2.19-Win32-VC15-x64/php.exe"

如果使用 Laragon...不要忘记将 \ 更改为 /

  "php.executablePath": "C:/laragon/bin/php/php-7.4.5-Win32-vc15-x64/php.exe",
  • Go to 'Preference' -> 'Settings'转到“首选项”->“设置”
  • Search for> php path搜索> php path
  • Then click Edit in settings.json然后点击Edit in settings.json
  • Then just put your directory with (DOUBLE Backslash).然后只需将您的目录与(双反斜杠)一起放置。

Here is an example of mine:这是我的一个例子:

C:\\php\\php.exe

Here is the format:这是格式:

drive:\\folder_name\\php.exe

This should fix the problem.这应该可以解决问题。

php.validate.executablePath in laragon laragon 中的 php.validate.executablePath

In my case using laragon is like this就我而言,使用 laragon 是这样的

  "php.validate.executablePath": "C:\\laragon\\bin\\php\\php-7.4.19-Win32-vc15-x64\\php.exe"
  • php-7.4.19-Win32-vc15-x64 : Note that this part of your file name may be different php-7.4.19-Win32-vc15-x64 :注意这部分你的文件名可能不同

Unix / Linux / Mac / WSL , Unix / Linux / Mac / WSL ,


Step 1 : Find PHP path第 1 步:查找 PHP 路径

whereis php

Response will show you where your php directories are.响应将显示您的 php 目录在哪里。

user@User:~$ whereis php

php: /usr/bin/php /usr/bin/php8.0 /usr/bin/php8.1 /usr/lib/php /etc/php /usr/share/php8.0-opcache 
/usr/share/php8.0-xml /usr/share/php8.1-curl /usr/share/php8.1-opcache /usr/share/php8.0-gd 
/usr/share/php7.0-gd /usr/share/php7.0-common /usr/share/php8.0-readline /usr/share/php8.0-mysql 
/usr/share/php8.0-zip /usr/share/php8.1-mbstring /usr/share/php8.1-xml /usr/share/php8.0-mbstring 
/usr/share/php8.0-common /usr/share/php8.0-curl /usr/share/php8.1-readline /usr/share/php8.1-common 
/usr/share/man/man1/php.1.gz

Step 2 : Configure VS-Code第 2 步:配置 VS 代码

Go to -> "Preference -> Settings"转到->“首选项->设置”
Search for -> "php path"搜索->“php 路径”
Then click -> "Edit in settings.json"然后点击->“在settings.json中编辑”


In my case I had multiple.就我而言,我有多个。
So I chose the one I wanted from the available versions and my VS-Code looked like this所以我从可用版本中选择了我想要的版本,我的 VS-Code 看起来像这样


"php.validate.executablePath": "/usr/bin/php8.1",

"php.validate.executablePath": "C:\xamp\php\php.exe" 这适用于这种情况

For Wampserver:对于 Wampserver:

Inside settings.json内部设置。json

"php.validate.executablePath": "C:/wamp64/bin/php/php7.4.26/php.exe"

If php is inside a docker container and you don't need it or don't want to install it on you local system, you can create a php wrapper script like it is suggested here: https://github.com/felixfbecker/vscode-php-intellisense/issues/471 If php is inside a docker container and you don't need it or don't want to install it on you local system, you can create a php wrapper script like it is suggested here: https://github.com/felixfbecker/ vscode-php-intellisense/issues/471

I prefer to use the remote container Microsoft extension on vscode and work inside the container itself.我更喜欢在 vscode 上使用远程容器 Microsoft 扩展并在容器本身内部工作。

I've used such solution:我用过这样的解决方案:

First, create a php file inside /usr/local/bin/ and make it executable:首先,在/usr/local/bin/中创建一个php文件并使其可执行:

sudo touch /usr/local/bin/php
sudo chmod +x /usr/local/bin/php

Update content of it with:更新内容:

docker exec $(docker ps --filter name=-app --format '{{.ID}}') php "$@"

So this will find a container with the name -app at the end.所以这会在最后找到一个名为-app的容器。 I'm using it along with the docker compose where I have php inside some container which has -app at the end of the name.我将它与 docker compose 一起使用,其中php在名称末尾有-app的某个容器中。 You can check more about filter here .您可以在此处查看有关filter的更多信息。

And then update php.validate.executablePath with /usr/local/bin/php :然后用/usr/local/bin/php更新php.validate.executablePath

{
    "php.validate.executablePath": "/usr/local/bin/php"
}

I also got this error message.我也收到此错误消息。 The path to php.exe was already set in my System Environment Variables. php.exe的路径已在我的系统环境变量中设置。

The error went away when I commented out "php.validate.executablePath": "c:/path/to/php7.2.10/php.exe" , in the settings.json file in VSCode.当我在 VSCode 的settings.json文件中注释掉"php.validate.executablePath": "c:/path/to/php7.2.10/php.exe"时,错误就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM