Home  >  Article  >  Backend Development  >  Minor [PHP Framework] 5. Events, minorphp framework events_PHP tutorial

Minor [PHP Framework] 5. Events, minorphp framework events_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:02993browse

Minor【PHP Framework】5. Event, minorphp framework event

5.1 Event Event

Minor’s Event class provides a simple observer implementation that allows you to subscribe to and listen for events in your application.

 5.1.1 Subscribe to events

First create an event class:

<?<span>php

namespace App\Event;

</span><span>use</span><span> Minor\Event\Event;

</span><span>class</span> DemoEvent <span>extends</span><span> Event
{
    </span><span>private</span> <span>$name</span><span>;

    </span><span>public</span> <span>function</span> __construct(<span>$name</span><span>)
    {
        </span><span>$this</span>->name = <span>$name</span><span>;
    }

    </span><span>public</span> <span>function</span> setName(<span>$name</span><span>)
    {
        </span><span>$this</span>->name = <span>$name</span><span>;
    }

    </span><span>public</span> <span>function</span><span> getName()
    {
        </span><span>return</span> <span>$this</span>-><span>name;
    }
}</span>

Then register this event in the configuration file:

<?<span>php
</span><span>return</span> <span>$events</span> =<span> [
    </span>'App\Event\DemoEvent'    =><span>    [
        </span>'App\Listener\DemoListener' => 'handle',<span>
    ]</span>,<span>
];</span>

 5.1.2 Trigger event

Minor provides an event management class: MinorEventEventManger. This event can be triggered by calling the static method fire: EventManager::fire($event) of this class, for example:

<span>class</span> FooController <span>extends</span><span> Controller
{
    </span><span>public</span> <span>function</span> bar(<span>$productName</span><span>)
    {
        </span><span>$event</span> = <span>new</span> DemoEvent('DemoEvent'<span>);
        EventManager</span>::fire(<span>$event</span><span>);
        </span>...<span>
    }
}</span>

5.2 Listener

When an event is triggered, the event manager EventManager will trigger the listener's formulation method through the configuration file. In the 5.1.1 configuration file, we configured the DemoEvent listener as the handle method of AppListenerDemoListener. You can take a look at the implementation of this class:

<?<span>php

namespace App\Listener;

</span><span>use</span><span> App\Event\DemoEvent;
</span><span>use</span><span> Minor\Event\Listener;

</span><span>class</span> DemoListener <span>extends</span><span> Listener
{
    </span><span>public</span> <span>function</span> handle(DemoEvent <span>$event</span><span>)
    {
        </span><span>echo</span> '[DemoListener] handle the event:[' . <span>$event</span>->getName() .'] success!<br/><br/>'<span>;
    }
}</span>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1135179.htmlTechArticleMinor【PHP Framework】5. Event, minorphp framework event 5.1 Event Event Minor's Event class provides a simple observation Orientation implementation allows you to subscribe to and listen for events in your application. 5....
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn