首页 > 其他 > 详细

laravel 事件的使用案例

时间:2016-10-26 14:15:12      阅读:246      评论:0      收藏:0      [点我收藏+]

以下是我对事件使用的一些记录

创建事件

执行以下命令,执行完成后,会在 app\Events 下面出现一个 DeleteEvent.php 文件,事件就在次定义

php artisan make:event DeleteEvent

  

编写事件
#DeleteEvent.php
<?php

namespace 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‘;
    }
}

  

 
创建监听listener

执行以下命令,执行完成后,会在 app\Listeners 下面出现一个 DeleteEventListener.php 文件,是对事件 DeleteEvent的监听

php artisan make:listener --event=DeleteEvent  DeleteEventListener

  

编写事件监听
#DeleteEventListener.php
<?php

namespace 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();
    }
}

  

 
调用事件-在控制器使用
#EventController.php
<?php

namespace 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());       //框架默认调用broadcastOn()

        $event = new DeleteEvent();     //自定义 
        event($event->broadcastOn());
    }
}

  

 
编写路由
#routes.php
Route::get(‘/event‘,[‘uses‘=>‘EventController@index‘]);

  

 

laravel 事件的使用案例

原文:http://www.cnblogs.com/zeopean/p/5999885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!