使用了laravel的db库,并且记录了db的sql日志,想要直接使用laravel的event库,按laravel的文档使用不了,有大神给下解决方案吗

大好时光

使用了laravel的db库,并且记录了db的sql日志,想要直接使用laravel的event库,按laravel的文档使用不了,有大神给下解决方案吗

1355 2 2
2个回答

2548a

https://www.workerman.net/a/1261
评论里有使用laravel 事件库的

yzh52521

laravel event事件使用

namespace support;

use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

/**
 *  class Event
 * @package support
 *
 *  Strings methods
 * @method static \Illuminate\Events\Dispatcher dispatch($event)
 */
class Event
{
    /**
     * @var Dispatcher
     */
    protected static $instance=null;

    /**
     * @return Dispatcher|null
     */
    public static function instance()
    {
        if (!static::$instance) {
            $container        = new Container;
            static::$instance = new Dispatcher($container);
            $eventsList       = config('events');
            if (isset($eventsList['listener']) && !empty($eventsList['listener'])) {
                foreach ($eventsList['listener'] as $event => $listener) {
                    if (is_string($listener)) {
                        $listener = implode(',', $listener);
                    }
                    foreach ($listener as $l) {
                        static::$instance->listen($event, $l);
                    }
                }
            }
            if (isset($eventsList['subscribe']) && !empty($eventsList['subscribe'])) {
                foreach ($eventsList['subscribe'] as  $subscribe) {
                    static::$instance->subscribe($subscribe);
                }
            }
        }
        return static::$instance;
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        return self::instance()->{$name}(... $arguments);
    }

}

配置
config/events.php

return [
    'listener'  => [
        app\events\Test::class => [
            \app\listeners\TestListeners::class,
        ],
    ],
    'subscribe' => [
        \app\subscribes\TestSubscribe::class,
    ],
];

事件类:Test

namespace app\events;

class Test
{
    public  $data = [];

    public function __construct($data)
    {
        $this->data = $data;
    }
}

监听类

namespace app\listeners;

use app\events\Test;

class TestListeners
{
    public function __construct()
    {
    }

    /**
     * 处理事件
     * @return void
     */
    public function handle(Test $event)
    {
        // 控制台打印
        var_dump('listener');
        var_dump($event->data);
    }
}

订阅类

namespace app\subscribes;

use app\events\Test;

class TestSubscribe
{
    public function handleTest(Test $event)
    {
        var_dump('subscribe');
        var_dump($event);
    }

    public function subscribe($events)
    {
        $events->listen(
            Test::class,
            [TestSubscribe::class, 'handleTest']
        );
    }
}

helpers.php 增加

/**
 * 事件
 * @param $event
 */
function event($event)
{
    Event::dispatch($event);
}

调用触发事件

event(new Test('event data'));
年代过于久远,无法发表回答
×
🔝