Use cases for Laravel events

Source: Internet
Author: User

Here are some of the records I used for the event

Create Event

Executes the following command, after execution completes, a deleteevent.php file appears under App\events, and the event is defined in the secondary

PHP Artisan make:event deleteevent

  

Writing events
#DeleteEvent.php
<?phpnamespace app\events;use app\events\event;use illuminate\queue\serializesmodels;use Illuminate\Contracts\ Broadcasting\shouldbroadcast;class Deleteevent extends event{use    serializesmodels;    Public function __construct ()    {        //    } public    function Broadcaston ()    {        print ' delete event ';    }}

  

 
Creating a listening listener

Execute the following command, after execution completes, a deleteeventlistener.php file appears under App\listeners, which is the listener for event Deleteevent

PHP artisan make:listener--event=deleteevent  Deleteeventlistener

  

Writing event listeners
#DeleteEventListener.php
<?phpnamespace app\listeners;use app\events\deleteevent;use illuminate\queue\interactswithqueue;use Illuminate\ Contracts\queue\shouldqueue;class deleteeventlistener{public    function __construct ()    {        //    }    Public function handle (deleteevent $event)    {        //        $event->broadcaston ();}    }

  

 
Invoke Event-the controller uses
#EventController.php
<?phpnamespace app\http\controllers;use app\events\deleteevent;use app\events\someevent;use Illuminate\Http\ Request;use app\http\requests;class Eventcontroller extends controller{    //Public    Function index ()    {//        Event (New Someevent ());       The framework defaults to calling Broadcaston ()        $event = new Deleteevent ();     Custom         Event ($event->broadcaston ());}    }

  

 
Writing a route
#routes.php
Route::get ('/event ', [' uses ' = ' [email protected] ']);

  

 

Use cases for Laravel events

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.