laravel 更改 Composer 在 Windows 上使用的 PHP 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39881924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:34:21  来源:igfitidea点击:

Change PHP version used by Composer on Windows

phplaravelnginxcomposer-phpwamp

提问by JMS786

I have already use WAMP 2.5 with PHP 5.5.12, and with Composer. The php is on:

我已经在 PHP 5.5.12 和 Composer 中使用了 WAMP 2.5。php 开启:

C:\wamp\bin\php\php5.5.12

For new project, I need to use nginx and installed PHP 7. The php is on:

对于新项目,我需要使用 nginx 并安装 PHP 7。 php 开启:

C:\nginx\php

Now, using GitBash MINGW32, I tried to install laravel 5.3 using Composer create-projectbut it said

现在,使用 GitBash MINGW32,我尝试使用 Composer 安装 laravel 5.3,create-project但它说

[InvalidArgumentException]
Could not find package laravel/laravel with version 5.3 in a version 
installable using your PHP version 5.5.12.

I already put both C:\wamp\bin\php\php5.5.12and C:\nginx\phpon Windows System PATH variable.

我已经把C:\wamp\bin\php\php5.5.12和 都C:\nginx\php放在了 Windows 系统 PATH 变量上。

How do I change the PHP version used by Composer?

如何更改 Composer 使用的 PHP 版本?

采纳答案by BVengerov

Three ways to do this, really.

三种方法可以做到这一点,真的。

Create an alias in .bashrcto always run composer with the corresponding version

创建别名.bashrc以始终使用相应版本运行 composer

Something like alias ncomposer=`/path/to/php /path/to/composer.phar `

就像是 alias ncomposer=`/path/to/php /path/to/composer.phar `

Specify the path to PHP version inside composer.pharitself

composer.phar自身内部指定 PHP 版本的路径

This is specified at the start of the file: #!/path/to/php php. Then composer should run with composer.phar

这是在文件开头指定的:#!/path/to/php php. 然后作曲家应该运行composer.phar

NB!The line will disappear upon self-update, so it's not a reliable solution.

注意!这条线会在自我更新时消失,所以这不是一个可靠的解决方案。

Move up the path with the newest PHP version

使用最新的 PHP 版本向上移动路径

If you place C:\nginx\phpfirst, it should be used by default when using composer.

如果C:\nginx\php先放置,则在使用 composer 时应默认使用它。

Hope this helps!

希望这可以帮助!

回答by w3spi

Although this question was solved, the answer didn't help me. I will explain how I managed to make composerto work in a version of PHP different from the one which is installed by default on my OS (PHP 7.1.1) as well as in my environment variables (these will not be changed !). Note that I'm using Xampp, but the principle remains the same for Wamp.

虽然这个问题已经解决了,但答案对我没有帮助。我将解释我如何设法让Composer在不同于默认安装在我的操作系统 (PHP 7.1.1) 以及我的环境变量中的 PHP 版本中工作(这些不会改变!)。请注意,我使用的是 Xampp,但 Wamp 的原理保持不变。

  • Starting from this answer:

    1. Start up Git Bash
    2. Type cd ~/to go to your home folder
    3. Type touch .bash_profileto create your new file.
    4. Edit .bash_profilewith your favorite editor
  • 这个答案开始:

    1. 启动 Git Bash
    2. 键入cd ~/以转到您的主文件夹
    3. 键入touch .bash_profile以创建新文件。
    4. 编辑.bash_profile带你喜欢的编辑器


  • In my case I have a folder named php733inside xampp folder which corresponds to PHP 7.3.3. This is this other answerthat helped me in creating the alias :

    alias composer733='/c/[xampp folder]/php733/php.exe /c/ProgramData/ComposerSetup/bin/composer.phar '
    

    Then, type . .bash_profileto reload .bash_profileand update any functions you add. Notice the space between the two dots!

  • 就我而言,我有一个名为php733xampp 文件夹内的文件夹,它对应于 PHP 7.3.3。这是另一个帮助我创建别名的答案

    alias composer733='/c/[xampp folder]/php733/php.exe /c/ProgramData/ComposerSetup/bin/composer.phar '
    

    然后,键入. .bash_profile以重新加载.bash_profile和更新您添加的任何功能。注意两个点之间的空间



Finally, type this command in Git Bash :

最后,在 Git Bash 中输入以下命令:

composer733 [whatever you wan]


Example: in the project that requires at least PHP 7.1.3

示例:在至少需要 PHP 7.1.3 的项目中

  1. Using composer:

    $ composer update
    This package requires php ^7.1.3 but your HHVM version does not satisfy that requirement.
    
  2. Using composer733(the alias I created) :

    $ composer733 update
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Package operations: 98 installs, 0 updates, 0 removals
      - Installing [...] (v1.11.0): Loading from cache
    
  1. 使用作曲家

    $ composer update
    This package requires php ^7.1.3 but your HHVM version does not satisfy that requirement.
    
  2. 使用composer733(我创建的别名):

    $ composer733 update
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Package operations: 98 installs, 0 updates, 0 removals
      - Installing [...] (v1.11.0): Loading from cache
    

It works, without having to change the environment variables

它可以工作,而无需更改环境变量