# 事件监听

事件监听可能是许多开发者比较关注的事情,丰富的事件监听可以让开发者更好的开发相关插件。有关 meedu 的事件注册,开发者可以到meedu的 EventServiceProvider 文件查看,这里给上一个 demo ,仅供参考:

<?php

/*
 * This file is part of the Qsnh/meedu.
 *
 * (c) XiaoTeng <616896861@qq.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        'App\Events\PaymentSuccessEvent' => [
            '\App\Listeners\PaymentSuccessEvent\OrderPaidDeliverListener',
            '\App\Listeners\PaymentSuccessEvent\OrderPaidNotificationListener',
            '\App\Listeners\PaymentSuccessEvent\OrderPaidStatusChangeListener',
            '\App\Listeners\PaymentSuccessEvent\PromoCodeListener',
            '\App\Listeners\PaymentSuccessEvent\InviteUserRewardListener',
        ],
        'App\Events\OrderCancelEvent' => [
            '\App\Listeners\OrderCancelEvent\PromoCodeResumeListener',
            '\App\Listeners\OrderCancelEvent\InviteBalanceResumeListener',
        ],
        'App\Events\AdFromEvent' => [
            'App\Listeners\AdFromEvent\AdFromListener',
        ],
        \SocialiteProviders\Manager\SocialiteWasCalled::class => [
            'SocialiteProviders\\WeixinWeb\\WeixinWebExtendSocialite@handle',
            'SocialiteProviders\\QQ\\QqExtendSocialite@handle',
        ],
        'App\Events\CourseCommentEvent' => [
            'App\Listeners\CourseCommentEvent\NotifyOwnerListener',
            'App\Listeners\CourseCommentEvent\AtEventListener',
        ],
        'App\Events\VideoCommentEvent' => [
            'App\Listeners\VideoCommentEvent\NotifyOwnerListener',
            'App\Listeners\VideoCommentEvent\AtEventListener',
        ],
        'App\Events\UserRegisterEvent' => [
            'App\Listeners\UserRegisterEvent\WelcomeMessageListener',
        ],
        'App\Events\UserLoginEvent' => [
            'App\Listeners\UserLoginEvent\SafeAlertListener',
            'App\Listeners\UserLoginEvent\BindMobileListener',
        ],
        'App\Events\UserInviteBalanceWithdrawCreatedEvent' => [
            'App\Listeners\UserInviteBalanceWithdrawCreatedEvent\NotifyListener',
        ],
        'App\Events\UserInviteBalanceWithdrawHandledEvent' => [
            'App\Listeners\UserInviteBalanceWithdrawHandledEvent\NotifyListener',
            'App\Listeners\UserInviteBalanceWithdrawHandledEvent\RefundBalanceListener',
        ],
    ];
}

插件中,我们可以这样监听某个事件:

<?php
/**
 * Created by PhpStorm.
 * User: xiaoteng
 * Date: 2019/1/13
 * Time: 12:07
 */

namespace Addons\Zhibo;

use Addons\Zhibo\Listeners\OrderPaidDeliverListener;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

class MainServiceProvider extends ServiceProvider
{

    protected $listen = [
        'App\Events\PaymentSuccessEvent' => [
            OrderPaidDeliverListener::class,
        ],
    ];

    public function boot()
    {
        // 事件注册
        foreach ($this->listen as $event => $listeners) {
            foreach ($listeners as $listener) {
                Event::listen($event, $listener);
            }
        }
    }

    public function register()
    {
    }

}

注意,事件监听的代码是写在 boot() 方法里面。