Laravel事件处理
php artisan make:event <event-class>
在这里,<event-class>应使用事件类的名称来代替。创建的类将被存储在 appEvents 目录。

php artisan handler:event <handler-class> --event = <event-class>
在这里,<event-class>应使用我们在步骤1来代替应,<handler-class> 创建事件类的名称使用处理程序类的名称来取代。新创建的处理程序类将被存储在appHandlersEvents 目录。
现在,我们需要注册该事件在文件 - appProvidersEventServiceProvier.php。 此文件包含一个数组:$listen。在这个数组,我们需要事件类添加作为键以及事件处理程序类作为它的值。
最后一步是触发使用事件门面触发事伯。fire()方法由事件类的对象调用。事件可以触发如下 -
Event::fire(<Event Class Object>);
实例
php artisan make:controller CreateStudentController


app/Http/Controllers/CreateStudentController.php
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use DB; use AppHttpRequests; use AppHttpControllersController; use AppEventsStudentAdded; use Event; class CreateStudentController extends Controller { public function insertform(){ return view('stud_add'); } public function insert(Request $request){ $name = $request->input('stud_name');$age = $request->input('stud_age');DB::insert('insert into student (name,age) values(?, ?)',[$name, $age]); echo "Record inserted successfully.<br/>"; echo '<a href = "/event">Click Here</a> to go back.'; //firing an event Event::fire(new StudentAdded($name)); } }
php artisan make:event StudentAdded


AppEventsStudentAdded.php
<?php namespace AppEvents; use AppEventsEvent; use IlluminateQueueSerializesModels; use IlluminateContractsBroadcastingShouldBroadcast; class StudentAdded extends Event { use SerializesModels; public $name; public function __construct($name) { $this->name = $name; } public function broadcastOn() { return []; } }
第9步 - 创建一个事件处理文件在 appHandlersEventsHandleNewStudentAdded.php,复制以下代码到该文件中。
appHandlersEventsHandleNewStudentAdded.php
<?php namespace AppHandlersEvents; use AppEventsStudentAdded; use IlluminateQueueInteractsWithQueue; use IlluminateContractsQueueShouldQueue; class HandleNewStudentAdded { protected $name; public function __construct() { // } public function handle(StudentAdded $event) { $this->name = $event->name; echo "<br><u>New Student added in database with name: </u>".$this->name; } }
第10步 - 现在,我们需要添加事件类和处理程序类存储在文件 - appProvidersEventServiceProvider.php
appProvidersEventServiceProvider.php
<?php namespace AppProviders; use IlluminateContractsEventsDispatcher as DispatcherContract; use IlluminateFoundationSupportProvidersEventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'AppEventsSomeEvent' => [ 'AppListenersEventListener', ], 'AppEventsStudentAdded' => [ 'AppHandlersEventsHandleNewStudentAdded', ], ]; /** * Register any other events for your application. * * @param IlluminateContractsEventsDispatcher $events * @return void */ public function boot(DispatcherContract $events) { parent::boot($events); // } }
app/Http/routes.php
Route::get('event','CreateStudentController@insertform'); Route::post('addstudent','CreateStudentController@insert');
http://localhost:8000/event


第14步 - 增加学生的姓名,然后点击“添加学生”按钮,将您重定向到下面的屏幕。看看灰色高亮行。 我们已经指定处理方法,在一个事件被触发执行HandleNewStudentAdded类的处理方法时添加此行。
