我想听一个包发起的事件(Laravel impersonate)。
当我设置我的EventServiceProvider时:
<?php
namespace App\Providers;
use App\Listeners\LogImpersonation;
use Illuminate\Support\Facades\Event;
use Lab404\Impersonate\Events\TakeImpersonation;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
TakeImpersonation::class => [
LogImpersonation::class,
]
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
我收到以下错误:
传递给App \ Listeners \ LogImpersonation :: handle()的参数1必须是App \ Events \ TakeImpersonation的实例,给出Lab404 \ Impersonate \ Events \ TakeImpersonation的实例
我的LogImpersonation:
<?php
namespace App\Listeners;
use App\Events\TakeImpersonation;
use App\User;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
class LogImpersonation
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param TakeImpersonation $event
* @return void
*/
public function handle(TakeImpersonation $event)
{
Log::info($event->impersonator->name . ' ingelogd als ' . $event->impersonated);
}
}
我无法想象我必须移动事件,这是我第一次尝试使用事件,所以我必须错过一些简单的事情。
错误消息告诉您到底出了什么问题:
传递给App \ Listeners \ LogImpersonation :: handle()的参数1必须是App \ Events \ TakeImpersonation的实例,给出Lab404 \ Impersonate \ Events \ TakeImpersonation的实例
所以你的App\Listeners\LogImpersonation::handle()
方法期待给出一个App\Events\TakeImpersonation
的实例,但是得到了Lab404\Impersonate\Events\TakeImpersonation
。
您需要更新侦听器类以导入正确的类。因此,在顶部的导入中,交换App\Events\TakeImpersonation
(这是错误的,在您的应用程序中不存在)Lab404\Impersonate\Events\TakeImpersonation
(您实际正在侦听的包事件的完全限定名称)。
看看qazxsw poi方法 - 它需要qazxsw poi参数类型为LogImpersonation@handle
。要解决该错误,必须从包的$event
派生此类。
如果您不需要扩展包事件类,则可以通过使用App\Events\TakeImpersonation
更改文件中的Lab404\Impersonate\Events\TakeImpersonation
语句来删除App\Events\TakeImpersonation
版本并使用包版本:
use