Error: While updating laravel 8 to 9. Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

Error: While updating laravel 8 to 9. Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

Nothing to install, update or remove Generating optimized autoload files Class App\Helpers\Helper located in C:/wamp64/www/vuexylaravel/app\Helpers\helpers.php does not comply with psr-4 autoloading standard. Skipping. > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi

   Error 

  Undefined constant Illuminate\Http\Request::HEADER_X_FORWARDED_ALL
  at C:\wamp64\www\vuexylaravel\vendor\fideloper\proxy\config\trustedproxy.php:48
     44▕      * - 'HEADER_X_FORWARDED_AWS_ELB' (If you are using AWS Elastic Load Balancer)
     45▕      *
     46▕      * @link https://symfony.com/doc/current/deployment/proxies.html
     47▕      */
  ➜  48▕     'headers' => Illuminate\Http\Request::HEADER_X_FORWARDED_ALL,
     49▕
     50▕ ];
     51▕

  1   C:\wamp64\www\vuexylaravel\vendor\laravel\framework\src\Illuminate\Support\ServiceProvider.php:138
      require()

  2   C:\wamp64\www\vuexylaravel\vendor\fideloper\proxy\src\TrustedProxyServiceProvider.php:28
      Illuminate\Support\ServiceProvider::mergeConfigFrom("C:\wamp64\www\vuexylaravel\vendor\fideloper\proxy\config\trustedproxy.php", "trustedproxy")
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

如果您要通过将现有应用程序代码导入全新的 Laravel 9 应用程序框架,将 Laravel 8 项目升级到 Laravel 9,您可能需要更新应用程序的“可信代理”中间件。

在您的 app/Http/Middleware/TrustProxies.php 文件中,将 use Fideloper\Proxy\TrustProxies as Middleware 更新为使用 Illuminate\Http\Middleware\TrustProxies 作为中间件

接下来,在 app/Http/Middleware/TrustProxies.php 内,您应该更新 $headers 属性 定义:

// 之前...

protected $headers = Request::HEADER_X_FORWARDED_ALL;

// 之后...

protected $headers =
    Request::HEADER_X_FORWARDED_FOR |
    Request::HEADER_X_FORWARDED_HOST |
    Request::HEADER_X_FORWARDED_PORT |
    Request::HEADER_X_FORWARDED_PROTO |
    Request::HEADER_X_FORWARDED_AWS_ELB;

then run composer update

确保您使用的是 PHP8.0

上面回答的同事原则上是对的。只是他忘了说,文件全部改完后,需要删除包fideloper/proxy:

https://laravel.com/docs/9.x/upgrade

Trusted Proxies

Likelihood Of Impact: Low

If you are upgrading your Laravel 8 project to Laravel 9 by importing your existing application code into a totally new Laravel 9 application skeleton, you may need to update your application's "trusted proxy" middleware.

Within your app/Http/Middleware/TrustProxies.php file, update use Fideloper\Proxy\TrustProxies as Middleware to use Illuminate\Http\Middleware\TrustProxies as Middleware.

Next, within app/Http/Middleware/TrustProxies.php, you should update the $headers property definition:

// Before...
protected $headers = Request::HEADER_X_FORWARDED_ALL;

// After...
protected $headers =
   Request::HEADER_X_FORWARDED_FOR |
   Request::HEADER_X_FORWARDED_HOST |
   Request::HEADER_X_FORWARDED_PORT |
   Request::HEADER_X_FORWARDED_PROTO |
   Request::HEADER_X_FORWARDED_AWS_ELB;

Finally, you can remove the fideloper/proxy Composer dependency from your application:

composer remove fideloper/proxy

如果上述答案对您不起作用并且您遇到相同的错误,请再执行一行以从 composer.json.

中删除 fideloper/proxy
// Before...
protected $headers = Request::HEADER_X_FORWARDED_ALL;
 
// After...
protected $headers =
    Request::HEADER_X_FORWARDED_FOR |
    Request::HEADER_X_FORWARDED_HOST |
    Request::HEADER_X_FORWARDED_PORT |
    Request::HEADER_X_FORWARDED_PROTO |
    Request::HEADER_X_FORWARDED_AWS_ELB;

这将从 composer.json 中删除代理,异常将消失。

composer remove fideloper/proxy

它在 laravel 9 升级指南中被正确提及。 https://laravel.com/docs/9.x/upgrade#the-assert-deleted-method

在遵循对我有用的 laravel 升级文档后我做了两件事:

  1. 手动进入 composer.json 并删除 fideloper/proxy

运行 作曲家更新。如果您仍然遇到问题。接下来关注:

  1. 检查此文件 TrustProxies.php 是否存在于 vendor\laravel\framework\src\Illuminate\Http\Middleware\TrustProxies。php 如果不存在,您可以从旧文件复制。

如果您要通过将现有应用程序代码导入全新的 Laravel 9 应用程序框架来将 Laravel 8 项目升级到 Laravel 9,我尝试了下面的解决方案。

a) 我通过删除“config/trustedproxy.php”修复了它。我这边 app\Http\Middleware 的目录中没有 TrustProxies 中间件