Laravel教程:事件处理方法与实例
php artisan make:event <event-class>
在这里,<event-class>应使用事件类的名称来代替。创建的类将被存储在 app\Events 目录。

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

app/Http/Controllers/CreateStudentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Events\StudentAdded;
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

App\Events\StudentAdded.php
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class StudentAdded extends Event {
use SerializesModels;
public $name;
public function __construct($name) {
$this->name = $name;
}
public function broadcastOn() {
return [];
}
}
第9步 - 创建一个事件处理文件在 app\Handlers\Events\HandleNewStudentAdded.php,复制以下代码到该文件中。
app\Handlers\Events\HandleNewStudentAdded.php
<?php
namespace App\Handlers\Events;
use App\Events\StudentAdded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
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步 - 现在,我们需要添加事件类和处理程序类存储在文件 - app\Providers\EventServiceProvider.php
app\Providers\EventServiceProvider.php
<?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 listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
'App\Events\StudentAdded' => [
'App\Handlers\Events\HandleNewStudentAdded',
],
];
/**
* Register any other events for your application.
*
* @param \Illuminate\Contracts\Events\Dispatcher $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类的处理方法时添加此行。





