Home > php教程 > php手册 > Minor [PHP Framework] 5. Events, minorphp framework events

Minor [PHP Framework] 5. Events, minorphp framework events

WBOY
Release: 2016-07-06 14:25:07
Original
1210 people have browsed it

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>
Copy after login

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>
Copy after login

 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>
Copy after login

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>
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template