【问题标题】:Laravel listening for package eventLaravel 监听包事件
【发布时间】:2018-07-19 11:14:55
【问题描述】:

我想监听由包 (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 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);
    }
}

我无法想象我必须移动事件,这是我第一次尝试使用事件,所以我一定错过了一些简单的东西。

【问题讨论】:

    标签: php laravel events listener


    【解决方案1】:

    错误消息告诉您准确出了什么问题:

    传递给 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(您实际监听的包事件的完全限定名称)。

    【讨论】:

      【解决方案2】:

      查看LogImpersonation@handle 方法——它接受$event 参数类型为App\Events\TakeImpersonation。为了解决这个错误,这个类必须从包的Lab404\Impersonate\Events\TakeImpersonation派生。

      如果您不需要扩展包事件类,那么您可以删除您的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
      {
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-28
        • 2020-11-29
        • 1970-01-01
        • 1970-01-01
        • 2022-06-21
        • 1970-01-01
        • 2017-10-25
        • 1970-01-01
        相关资源
        最近更新 更多