Laravel 侦听包事件
Laravel listening for package event
我想监听包 (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();
//
}
}
我收到以下错误:
Argument 1 passed to App\Listeners\LogImpersonation::handle() must be
an instance of App\Events\TakeImpersonation, instance of
Lab404\Impersonate\Events\TakeImpersonation given
我的日志模拟:
<?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);
}
}
我无法想象我必须移动事件,这是我第一次尝试使用事件,所以我一定是遗漏了一些简单的东西。
错误消息确切地告诉你出了什么问题:
Argument 1 passed to App\Listeners\LogImpersonation::handle() must be an instance of App\Events\TakeImpersonation, instance of Lab404\Impersonate\Events\TakeImpersonation given
所以你的 App\Listeners\LogImpersonation::handle()
方法期望得到 App\Events\TakeImpersonation
的实例,但却得到了 Lab404\Impersonate\Events\TakeImpersonation
.
您需要更新监听器 class 以导入正确的 class。因此,在顶部的导入中,将 App\Events\TakeImpersonation
(这是错误的并且不会存在于您的应用程序中)交换为 Lab404\Impersonate\Events\TakeImpersonation
(您实际正在监听的包事件的完全限定名称) .
查看 LogImpersonation@handle
方法 - 它采用 $event
类型的参数 App\Events\TakeImpersonation
。要解决这个 class 必须从包的 Lab404\Impersonate\Events\TakeImpersonation
.
派生的错误
如果您不需要扩展包事件 class 那么您可以删除您的 App\Events\TakeImpersonation
版本并通过更改文件中的 use
语句使用包版本 LogImpersonation
:
<?php
namespace App\Listeners;
use Lab404\Impersonate\Events\TakeImpersonation; // here
use App\User;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
class LogImpersonation
{
我想监听包 (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();
//
}
}
我收到以下错误:
Argument 1 passed to App\Listeners\LogImpersonation::handle() must be an instance of App\Events\TakeImpersonation, instance of Lab404\Impersonate\Events\TakeImpersonation given
我的日志模拟:
<?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);
}
}
我无法想象我必须移动事件,这是我第一次尝试使用事件,所以我一定是遗漏了一些简单的东西。
错误消息确切地告诉你出了什么问题:
Argument 1 passed to App\Listeners\LogImpersonation::handle() must be an instance of App\Events\TakeImpersonation, instance of Lab404\Impersonate\Events\TakeImpersonation given
所以你的 App\Listeners\LogImpersonation::handle()
方法期望得到 App\Events\TakeImpersonation
的实例,但却得到了 Lab404\Impersonate\Events\TakeImpersonation
.
您需要更新监听器 class 以导入正确的 class。因此,在顶部的导入中,将 App\Events\TakeImpersonation
(这是错误的并且不会存在于您的应用程序中)交换为 Lab404\Impersonate\Events\TakeImpersonation
(您实际正在监听的包事件的完全限定名称) .
查看 LogImpersonation@handle
方法 - 它采用 $event
类型的参数 App\Events\TakeImpersonation
。要解决这个 class 必须从包的 Lab404\Impersonate\Events\TakeImpersonation
.
如果您不需要扩展包事件 class 那么您可以删除您的 App\Events\TakeImpersonation
版本并通过更改文件中的 use
语句使用包版本 LogImpersonation
:
<?php
namespace App\Listeners;
use Lab404\Impersonate\Events\TakeImpersonation; // here
use App\User;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
class LogImpersonation
{