Laravel 9 composer 语法错误,意外标记“)”

Laravel 9 composer syntax error, unexpected token ")"

在我的 Laravel 9 项目中 运行 composer update 我遇到了这个错误信息:

Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 1 removal
  - Downloading fruitcake/laravel-cors (v3.0.0)
  - Removing asm89/stack-cors (v2.1.1)
  - Upgrading fruitcake/laravel-cors (v2.2.0 => v3.0.0): Extracting archive
77 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

In Finder.php line 588:

  syntax error, unexpected token ")"


Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

似乎更新已完成,但出了点问题,现在在控制台中我在 Finder.php:588 上收到 syntax error, unexpected token ")" 错误。

我检查了这个文件,它看起来是正确的。

当我在浏览器中打开我的项目时出现此错误:

Fatal error: Uncaught RuntimeException: A facade root has not been set. in /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:334
Stack trace:
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/RegisterErrorViewPaths.php(18): Illuminate\Support\Facades\Facade::__callStatic('replaceNamespac...', Array)
#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(626): Illuminate\Foundation\Exceptions\RegisterErrorViewPaths->__invoke()
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(607): Illuminate\Foundation\Exceptions\Handler->registerErrorViewPaths()
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(538): Illuminate\Foundation\Exceptions\Handler->renderHttpException(Object(Symfony\Component\HttpKernel\Exception\HttpException))
#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(444): Illuminate\Foundation\Exceptions\Handler->prepareResponse(Object(Illuminate\Http\Request), Object(Symfony\Component\HttpKernel\Exception\HttpException))
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(364): Illuminate\Foundation\Exceptions\Handler->renderExceptionResponse(Object(Illuminate\Http\Request), Object(ParseError))
#6 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(427): Illuminate\Foundation\Exceptions\Handler->render(Object(Illuminate\Http\Request), Object(ParseError))
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(115): Illuminate\Foundation\Http\Kernel->renderException(Object(Illuminate\Http\Request), Object(ParseError))
#8 /var/www/html/public/index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#9 {main}
  thrown in /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 334

我清除了 bootstrap/cahce/storage/framework/cache/data/storage/framework/sessions/storage/framework/views/ 文件夹中的所有缓存。

我使用PHP版本8.0.16.

Composer 版本 2.0.12 2021-04-01 10:14:59.

知道如何解决这个问题吗?

我做到了:

composer require --ignore-platform-reqs symfony/finder 6.1

我现在也有类似的问题:

[2022-05-30 17:52:03] laravel.ERROR: syntax error, unexpected token ")" {"exception":"[object] (ParseError(code: 0): syntax error, unexpected token \")\" at /vendor/symfony/finder/Finder.php:588)
[stacktrace]
#0 /vendor/composer/ClassLoader.php(346): Composer\Autoload\includeFile()
#1 /vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php(88): Composer\Autoload\ClassLoader->loadClass()
#2 /vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php(65): Illuminate\Foundation\Bootstrap\LoadConfiguration->getConfigurationFiles()
#3 /vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php(39): Illuminate\Foundation\Bootstrap\LoadConfiguration->loadConfigurationFiles()
#4 /vendor/laravel/framework/src/Illuminate/Foundation/Application.php(239): Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap()
#5 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(310): Illuminate\Foundation\Application->bootstrapWith()
#6 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(127): Illuminate\Foundation\Console\Kernel->bootstrap()
#7 /artisan(37): Illuminate\Foundation\Console\Kernel->handle()
#8 {main}
"}

所以我认为是 symfony/finder 的版本与 php 8.0 不兼容,尝试将任何与 symfony 相关的包降级到 6.0.x,因为我不认为您直接需要 symfony/finder,但其他一些包如 symfony/http-client

我认为您可能在某些时候使用 php 8.1 安装了依赖项并且您得到了错误的软件包版本,或者您可能安装了 laravel 10 而不是 laravel 9 ?

我找到一个解决方案:

  1. 升级到 PHP 8.1
  2. 运行 docker pull composer 升级最新版本的作曲家(从 2.0 到 2.2)
  3. 运行 composer update 在 Docker 容器中

要继续使用 php 8.0,请将这些条目添加到您的 composer.json

    "require": {
        ...
        "symfony/console": "6.0.*",
        "symfony/error-handler": "6.0.*",
        "symfony/finder": "6.0.*",
        "symfony/http-foundation": "6.0.*",
        "symfony/http-kernel": "6.0.*",
        "symfony/mailer": "6.0.*",
        "symfony/mime": "6.0.*",
        "symfony/process": "6.0.*",
        "symfony/routing": "6.0.*",
        "symfony/var-dumper": "6.0.*",
        "symfony/event-dispatcher": "6.0.*",
        "symfony/string": "6.0.*",
        "symfony/translation": "6.0.*",
        "symfony/translation-contracts": "3.0.*",
        "symfony/service-contracts": "3.0.*",
        "symfony/event-dispatcher-contracts": "3.0.*",
        "symfony/deprecation-contracts": "3.0.*",
        ...
    }

使用 php 8.0 和 Laravel 9 为我工作。