Home > Backend Development > PHP Tutorial > PHP framework Minor5 event (with code)

PHP framework Minor5 event (with code)

PHPz
Release: 2018-10-12 09:31:35
Original
1281 people have browsed it

5.1 Event

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

5.1.1 Subscribe to events

First create an event class:

<?php

namespace App\Event;use Minor\Event\Event;class DemoEvent extends Event
{private$name;publicfunction __construct($name)
    {$this->name = $name;
    }publicfunction setName($name)
    {$this->name = $name;
    }publicfunction getName()
    {return$this->name;
    }
}
Copy after login

Then register this event in the configuration file:

<?phpreturn$events = [&#39;App\Event\DemoEvent&#39;    =>    ['App\Listener\DemoListener' => 'handle',    ],];
Copy after login

5.1.2 Trigger events

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

class FooController extends Controller
{publicfunction bar($productName)
    {$event = new DemoEvent('DemoEvent');
        EventManager::fire($event);...    }
}
Copy after login

5.2 Listener

When the event is triggered, the event manager EventManager The listener's formulation method will be triggered 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:

<?php

namespace App\Listener;use App\Event\DemoEvent;use Minor\Event\Listener;class DemoListener extends Listener
{publicfunction handle(DemoEvent $event)
    {echo &#39;[DemoListener] handle the event:[&#39; . $event->getName() .'] success!

';
    }
}
Copy after login

The above introduces the content of the Minor5 event of the PHP framework (with code) , I hope it will be helpful to friends who are interested in PHP tutorials.


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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template