如何在 Windows 上正确地将 Symfony 添加到 Path?

问题描述:

我按照入门页面 (https://symfony.com/doc/current/setup.html).

I followed the instructions as per the Getting Started page (https://symfony.com/doc/current/setup.html).

所以在运行 php -r "readfile('https://symfony.com/installer');"

然后我在 C 驱动器的根目录中有一个名为 symfony 的文件.

I then had a file called symfony in the root of my C drive.

然后我将它移到了我的 C:\wamp64\bin\php 文件夹中.

I then moved this to be in my C:\wamp64\bin\php folder.

C:\wamp64\bin\php 添加到我的路径并关闭然后重新打开控制台(刷新路径,如果我运行 echo %path% 我可以看到它已添加到路径中).然后我尝试运行 symfony 并收到消息'symfony' 不被识别为内部或外部命令,可运行的程序或批处理文件..

Adding C:\wamp64\bin\php to my Path and closing then reopening the console (to refresh the path, if I run echo %path% I can see it has been added to the path). I then try running symfony and I get the message that 'symfony' is not recognized as an internal or external command, operable program or batch file..

如果我尝试在它所在的目录中运行 symfony,我会收到相同的消息.

I get the same message if I try running symfony in the directory it exists in.

如果我移动到包含 symfony 的目录并运行 php symfony 它可以工作,但是在不包含 symfony 的目录中使用时会失败代码>.

If I move to the directory containing symfony and run php symfony it works, but this fails when used in a directory that does not contain symfony.

如果我使用 GitBash 并尝试运行 symfony 那么它会按预期工作.

If I use GitBash and try running symfony then it works as expected.

为什么控制台在我的路径上找不到 symfony 以及如何将它添加到它以便它可以正确找到它.

Why does the console not find symfony on my Path and how can I add it to it so that it will find this correctly.

我使用的是 Windows 10 并安装了 WAMP.

I am using Windows 10 and have WAMP installed.

您需要添加一个批处理脚本来帮助窗口.

为了完整起见,以下是在 Windows 10 上测试所需的所有步骤:

For the sake of completeness, here are all steps required, tested on Windows 10:

  1. 获取symfony文件php -r "readfile('https://symfony.com/installer');"

symfony 文件移动到您选择的目录,因此在您的情况下 C:\wamp64\bin\php - 我个人制作了一个专用文件夹用于避免意外程序被拾取的命令:C:\commands

move the symfony file to a directory of your choice, so in your case C:\wamp64\bin\php - personally I made a dedicated folder for commands to avoid unintended programs being picked up on: C:\commands

确保目录,不是文件,已经添加到路径

此时如果你在命令提示符中输入symfony,它会询问你想用什么来执行程序,所以用处不大.

At this point if you enter symfony into the command prompt it will come up asking you what you want to execute the program with, so not much use.

为了解决这个问题,我们需要帮助它并告诉 windows 如何处理该文件——也就是说,让 PHP 运行它.

To remedy this we need to help it along and tell windows what to do with the file - that is, have PHP run it.

我们可以使用批处理脚本来实现:

We can achieve this using a batch script:

  1. 创建一个名为 symfony.bat 的文件,将它放在与您的 symfony 文件相同的目录中.

  1. create a file called symfony.bat, place it into the same directory as your symfony file.

在文件编辑器中打开批处理脚本并添加以下代码:

Open the batch script in a file editor and add the following code:

@ECHO OFF
php "%~dp0symfony" %*

一旦保存,symfony 命令应该可以正确运行,因为 Windows 现在将在输入命令时使用批处理文件.

Once saved the symfony command should run correctly as Windows will now use the batch file whenever the command is entered.