在安装php event扩展的服务器上运行lumen框架是会报错:

  1. [ErrorException]
  2. Cannot declare class Event, because the name is already in use

详细错误信息如下图所示:
image.png

  1. [2020-06-28 17:32:06] local.ERROR: ErrorException: Cannot declare class Event, because the name is already in use in /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php:729
  2. Stack trace:
  3. #0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(2, 'Cannot declare ...', '/alidata/www/yu...', 729, Array)
  4. #1 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(729): class_alias('Illuminate\\Supp...', 'Event')
  5. #2 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(696): Laravel\Lumen\Application->withAliases(Array)
  6. #3 /alidata/www/yunliao_lumen_pre/bootstrap/app.php(26): Laravel\Lumen\Application->withFacades()
  7. #4 /alidata/www/yunliao_lumen_pre/public/index.php(15): require('/alidata/www/yu...')
  8. #5 {main} {"exception":"[object] (ErrorException(code: 0): Cannot declare class Event, because the name is already in use at /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php:729)
  9. [stacktrace]
  10. #0 [internal function]: Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(2, 'Cannot declare ...', '/alidata/www/yu...', 729, Array)
  11. #1 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(729): class_alias('Illuminate\\\\Supp...', 'Event')
  12. #2 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(696): Laravel\\Lumen\\Application->withAliases(Array)
  13. #3 /alidata/www/yunliao_lumen_pre/bootstrap/app.php(26): Laravel\\Lumen\\Application->withFacades()
  14. #4 /alidata/www/yunliao_lumen_pre/public/index.php(15): require('/alidata/www/yu...')
  15. #5 {main}
  16. "}

错误原因:

在lumen框架中文件 vendor/laravel/lumen-framework/src/Application.php 749行 withAliases(),这里为 \Illuminate\Support\Facades\Event 定义了别名’Event’。导致与php event扩展的类名冲突

  1. \Illuminate\Support\Facades\Event::class => 'Event',

解决方法

bootstrap/app.php 中添加以下代码:

  1. $app->withFacades(true, [
  2. \Illuminate\Support\Facades\Event::class => 'LumenEvent',
  3. ]);

注意在这个文件中只能添加一次 $app->withFacades() 要引用的多个放到后面数组中。