Use of the Laravel 5 event

Source: Internet
Author: User

Event classes are typically stored in the App/events directory, and their handlers are stored in the App/handlers/events directory.

Creation of EventsLet's use artisan to create an event, such as the name Cqhtestevent
PHP Artisan make:event cqhtestevent
The resulting events are as follows
<?php namespace App\events;use app\events\event;use illuminate\queue\serializesmodels;class CqhTestEvent extends Event {Use serializesmodels;/** * Create a new event instance. * @return void */public function __construct () {}}
triggering of events
    • method One : You can directly use the event () function to trigger events, as follows
Event (New Cqhtestevent ());
    • method Two : You can also use the Event facade to trigger, as follows
$response = Event::fire (New cqhtestevent ());
The fire method returns an array of responses that you can use to control how your application will react next. Handling of EventsThe method of event processing can be two kinds, namely class and closure. Here in my processing class called Cqhtesteventhandler, let's use artisan to create this processing class
PHP Artisan handler:event Cqhtesteventhandler--event=cqhtestevent
The cqhtesteventhandler.php is then generated under the App/handlers/events file, with the following structure
<?php namespace App\handlers\events;use app\events\cqhtestevent;use illuminate\queue\interactswithqueue;use Illuminate\contracts\queue\shouldbequeued;class Cqhtesteventhandler {/** * Create the event handler. * @return void */pu Blic function __construct () {//}/** * Handle the event. * * @param  cqhtestevent  $event * @return void */public function handle (cqhtestevent $event) {//}}
And then we add a sentence to the handle method.
Public function handle (cqhtestevent $event) {//echo ' Chenqionghe event is processed ';}
In this way, the processing of our simple correspondence event is completed then, there are two ways to specify the processing class for the corresponding event, and the specified and Event::listen () methods in the Event service specify method One: Specify the class to be processed
    • In the boot method of Eventserviceprovider, specify that you want to process
Then, we need to register this event in the system service inside, in the App/providers folder there is a eventserviceprovider.php us, open him, find the corresponding listen attribute, plus the following
<?php namespace App\providers;use illuminate\contracts\events\dispatcher as Dispatchercontract;use Illuminate\ Foundation\support\providers\eventserviceprovider as Serviceprovider;class Eventserviceprovider extends serviceprovider {/** * The event handler mappings for the application. * * @var array */protected $listen = [' Event.name ' = = [' EventListener ',],        //Here add ' app\events\cqhtestevent ' = [' App\handlers\events\cqhtesteventhandler ',] ,];...}
Add the event here and define the class that handles the event (note that you can use more than one processing class for the event, just add it to the array)
    • In the Event::listen () method, specify
Event::listen (' app\events\cqhtestevent ', ' App\handlers\events\cqhtesteventhandler ');
Note: When specifying classes and routing is a principle, event handling by default calls Cqhtesteventhandler's handle method, if you want to specify the appropriate method, you can add the @ method name, such as [email protected] You only need to specify before invoking the event. method Two: Use closures to handle eventsWe cannot handle an event directly with a closure, as long as it is defined prior to triggering the event, as in the boot method of Eventserviceprovider
Event::listen (' App\events\cqhtestevent ', function ($event) {      //handle event ...});
If more than one of these event handlers is defined, they will be executed after triggering the event, which is equivalent to the Bind method in jquery.
    Public Function Gettest ()    {        event::listen (' app\events\cqhtestevent ', function ($event)        {            echo ') Trigger Event 1 ';        });        Event::listen (' App\events\cqhtestevent ', function ($event)        {            echo ' trigger event 2 ';        });        Event::listen (' App\events\cqhtestevent ', function ($event)        {            echo ' trigger event 3 ';        });        Event (New Cqhtestevent ());    }
The above three processing of the Cqhtestevent event is registered and the result is as follows
Triggering event 1 triggering event 2 triggering event 3
Again, like jquery, there are times when you want to stop passing events to other listeners. You can do this by return false from the handler
    Public Function Gettest ()    {        event::listen (' app\events\cqhtestevent ', function ($event)        {            echo ') Trigger Event 1 ';        });        Event::listen (' App\events\cqhtestevent ', function ($event)        {            echo ' trigger event 2 ';            return false;        });        Event::listen (' App\events\cqhtestevent ', function ($event)        {            echo ' trigger event 3 ';        });        Event (New Cqhtestevent ());    }
The operation results are as follows
Triggering event 1 triggering event 2

Use of the Laravel 5 event

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.