在安装php event扩展的服务器上运行lumen框架是会报错:
[ErrorException]
Cannot declare class Event, because the name is already in use
详细错误信息如下图所示:
[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
Stack trace:
#0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(2, 'Cannot declare ...', '/alidata/www/yu...', 729, Array)
#1 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(729): class_alias('Illuminate\\Supp...', 'Event')
#2 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(696): Laravel\Lumen\Application->withAliases(Array)
#3 /alidata/www/yunliao_lumen_pre/bootstrap/app.php(26): Laravel\Lumen\Application->withFacades()
#4 /alidata/www/yunliao_lumen_pre/public/index.php(15): require('/alidata/www/yu...')
#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)
[stacktrace]
#0 [internal function]: Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(2, 'Cannot declare ...', '/alidata/www/yu...', 729, Array)
#1 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(729): class_alias('Illuminate\\\\Supp...', 'Event')
#2 /alidata/www/yunliao_lumen_pre/vendor/laravel/lumen-framework/src/Application.php(696): Laravel\\Lumen\\Application->withAliases(Array)
#3 /alidata/www/yunliao_lumen_pre/bootstrap/app.php(26): Laravel\\Lumen\\Application->withFacades()
#4 /alidata/www/yunliao_lumen_pre/public/index.php(15): require('/alidata/www/yu...')
#5 {main}
"}
错误原因:
在lumen框架中文件 vendor/laravel/lumen-framework/src/Application.php 749行 withAliases(),这里为 \Illuminate\Support\Facades\Event 定义了别名’Event’。导致与php event扩展的类名冲突
\Illuminate\Support\Facades\Event::class => 'Event',
解决方法
在 bootstrap/app.php
中添加以下代码:
$app->withFacades(true, [
\Illuminate\Support\Facades\Event::class => 'LumenEvent',
]);
注意在这个文件中只能添加一次 $app->withFacades() 要引用的多个放到后面数组中。