symplify/symfony-event-dispatcher

此包已被废弃且不再维护。作者建议使用 contributte/event-dispatcher 包代替。
此包的最新版本(v2.0.0-RC2)没有可用的许可证信息。

Symfony\EventDispatcher 与 Nette 的集成。

v2.0.0-RC2 2017-04-27 14:57 UTC

This package is auto-updated.

Last update: 2022-03-08 10:48:48 UTC


README

Build Status Code Coverage Downloads

安装

composer require symplify/symfony-event-dispatcher

config.neon 中注册扩展

# app/config/config.neon

extensions:
	- Symplify\SymfonyEventDispatcher\Adapter\Nette\DI\SymfonyEventDispatcherExtension

使用

查看关于 EventDispatcher 的简短文章 http://pehapkari.cz/blog/2016/12/05/symfony-event-dispatcher该文章已测试 – 它将与 Symfony 4+ 保持最新。

1. 创建一个实现 Symfony\Component\EventDispatcher\SubscriberInterface 的类

// app/EventSubscriber/CheckRequestEventSubscriber.php

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent

final class CheckRequestEventSubscriber implements EventSubscriberInterface
{
    /**
     * @var bool
     */
    public $isUserNotified = false;

    public static function getSubscribedEvents(): array
    {
        // in format ['event name' => 'public function name that will be called']
        return [KernelEvents::REQUEST => 'validateRequest'];
    }


    // Appropriate event object is passed in arguments
    public function validateRequest(GetResponseEvent $event): void
    {
        // some logic to send notification
        $this->isUserNotified = true;
    }
}

2. 在服务中注册它

# app/config/config.neon

services:
    - App\EventSubscriber\CheckRequestEventSubscriber

然后它就工作了 :)

这就完了!

贡献

向主仓库发送 问题拉取请求