從字面上就能夠看出來,無非就是監聽一個事件。php
例句一個物流發貨的場景:我有個小程序商城,用戶下單後須要在後臺發貨,發完貨後我想在微信發送個模塊通知提示用戶,我想了想用戶不登陸微信或者把消息提示關閉了,那豈不是不能實時通知用戶已經發貨了,我決定再加個短信通知用戶。有了模塊通知,短信通知後我還嫌不夠...而後我繼續加.... 一般咱們會這麼寫:laravel
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class OrderController extends Controller { public function sendGoods() { //@todo:發貨邏輯 //@todo:模塊通知邏輯 //@todo:短信通知邏輯 //@todo:等等.... } }
這樣寫不是不能夠,可是邏輯功能愈來愈多,控制器只會變得臃腫起來,後期不便於維護。這時候可能會有人說,那我能夠封裝起來啊,是的,你能夠封裝起來,而後就能夠用簡短的代碼來實現。可是實際項目中,會涉及多人開發,這樣就不方便了。因此推薦使用laravel自帶的「事件監聽「... 小程序
3.1 打開終端->切換到項目根目錄->使用artisan命令建立事件監聽文件微信
php artisan make:event OrderEvent
執行完命令,發現 app/Events 目錄下多了個 OrderEvent.php 文件app
咱們能夠打開看看...ide
<?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Queue\SerializesModels; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class OrderEvent { use Dispatchable, InteractsWithSockets, SerializesModels; /** * Create a new event instance. * * @return void */ public function __construct() { // } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new PrivateChannel('channel-name'); } }
簡單的修改下構造函數,由於到時候咱們可能會 傳入 購買商品的 用戶函數
<?php namespace App\Events; use App\User; use Illuminate\Broadcasting\Channel; use Illuminate\Queue\SerializesModels; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class OrderEvent { use Dispatchable, InteractsWithSockets, SerializesModels; public $user; /** * Create a new event instance. * * @return void */ public function __construct(User $user) { $this->user = $user; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new PrivateChannel('channel-name'); } }
3.2 綁定事件學習
打開 app/Providers/EventServiceProvider.php 文件,找到 成員屬性 listen下:優化
protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], ];
把事件關聯起來,修改成:this
protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], 'App\Events\OrderEvent' => [ 'App\Listeners\sendModel', 'App\Listeners\sendPhone', ] ];
接着打開終端,執行 artisan 命令 生成 監聽文件
php artisan event:generate
而後咱們能夠在 app/Listeners 文件下發現多了兩個文件
打開這兩個文件,依次修改成:
sendModel.php:
<?php namespace App\Listeners; use App\Events\OrderEvent; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class sendModel { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param OrderEvent $event * @return void */ public function handle(OrderEvent $event) { $user = $event->user; // @todo: 當前用戶 //@todo:發送消息模板邏輯 dump('發送消息模板成功'); } }
sendPhone.php:
<?php namespace App\Listeners; use App\Events\OrderEvent; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class sendPhone { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param OrderEvent $event * @return void */ public function handle(OrderEvent $event) { $user = $event->user; // @todo: 當前用戶 //@todo:發送短信消息 dump('發送短信消息成功'); } }
3.3 最後咱們再打開 OrderController.php 修改成:
<?php namespace App\Http\Controllers; use App\Events\OrderEvent; use App\User; use Illuminate\Http\Request; class OrderController extends Controller { public function sendGoods() { $user = User::find(1); //@todo:發貨邏輯 dump('發貨成功!!'); event(new OrderEvent($user)); } }
3.4 綁定路由
Route::get('/sendGoods','OrderController@sendGoods');
3.5 訪問路由結果:
這樣是否是簡單,便於維護了許多呢... 還不會的小夥伴趕忙去試試吧!若是對你有幫助,請給個贊,謝謝!