国内如何用Composer安装Laravel框架

上下文

几天前在安装Laravel 11时遇到了一个问题,如下为Composer输出的报错信息:

Problem 1
    - laravel/laravel[v11.1.0, ..., v11.1.1] require laravel/framework ^11.9 -> satisfiable by laravel/framework[v11.9.0, v11.9.1, v11.9.2].
    - laravel/laravel[v11.0.0, ..., v11.0.9] require laravel/framework ^11.0 -> satisfiable by laravel/framework[v11.0.0, ..., v11.9.2].
    - laravel/framework[v11.0.0, ..., v11.9.2] require fruitcake/php-cors ^1.3 -> found fruitcake/php-cors[dev-feat-setOptions, dev-master, dev-main, dev-test-8.2, v0.1.0, v0.1.1, v0.1.2, v1.0-alpha1, ..., 1.2.x-dev (alias of de
v-master)] but it does not match the constraint.
    - Root composer.json requires laravel/laravel 11.* -> satisfiable by laravel/laravel[v11.0.0, ..., v11.1.1].

可见,因为无法找到至少1.3版本的fruitcake/php-cors,导致我无法安装laravel/laravel

解决方法

搜索了很多文章,有一个共同的说法是说该依赖无法从国内安装,需要在官方源和国内镜像之间。但是无论怎么操作,官方源与其说是慢,不如说是几乎没有动静,唯一的反应就是“连接超时”。
这些文章设置的源总是只有一个,我便突然想到,有没有可能repositories作为复数单词,里面可以设置很多个源。既然官方源慢,国内镜像缺失,那我干脆结合起来。
最后产生的composer.json内容长这样的:

{
  "repositories": {
    "packagist": {
      "type": "composer",
      "url": "https://repo.packagist.org"
    },
    "sjtu-packgist": {
      "type": "composer",
      "url": "https://mirrors.sjtug.sjtu.edu.cn/packagist/"
    },
    "aliyun-packgist": {
      "type": "composer",
      "url": "https://mirrors.aliyun.com/composer/"
    },
    "renrun-packagist": {
      "type": "composer",
      "url": "https://packagist.phpcomposer.com"
    }
  },
  "require": {
    "laravel/laravel": "11.*"
  }
}

以上仓库除了官方源,可以自行调整顺序和镜像源。
最后,运行composer install,就成功了。

注意:官方源要放在最前面,不然会产生以下错误:

Problem 1
    - laravel/laravel[v11.1.0, ..., v11.1.1] require laravel/framework ^11.9 -> satisfiable by laravel/framework[v11.9.0, v11.9.1, v11.9.2].
    - laravel/laravel[v11.0.0, ..., v11.0.9] require laravel/framework ^11.0 -> satisfiable by laravel/framework[v11.0.0, ..., v11.9.2].
    - laravel/framework[v11.0.0, ..., v11.9.2] require fruitcake/php-cors ^1.3 -> satisfiable by fruitcake/php-cors[v1.3.0] from composer repo (https://repo.packagist.org) but fruitcake/php-cors[dev-feat-setOptions, dev-master, 
dev-main, dev-test-8.2, v0.1.0, v0.1.1, v0.1.2, v1.0-alpha1, ..., 1.2.x-dev (alias of dev-master)] from composer repo (https://mirrors.aliyun.com/composer) has higher repository priority. The packages from the higher priority re
pository do not match your constraint and are therefore not installable. That repository is canonical so the lower priority repo's packages are not installable. See https://getcomposer.org/repoprio for details and assistance.   
    - Root composer.json requires laravel/laravel 11.* -> satisfiable by laravel/laravel[v11.0.0, ..., v11.1.1].
posted @ 2024-08-03 14:08  Orange233  阅读(205)  评论(0编辑  收藏  举报