标签:记录 hand listener model isa str on() name tco
以下是我对事件使用的一些记录
执行以下命令,执行完成后,会在 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‘; } }
执行以下命令,执行完成后,会在 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‘]);
标签:记录 hand listener model isa str on() name tco
原文地址:http://www.cnblogs.com/zeopean/p/5999885.html