<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class OrderController extends Controller { public function sendGoods() { //@todo:发货逻辑 //@todo:模块通知逻辑 //@todo:短信通知逻辑 //@todo:等等.... } }
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'); } }
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'); } }
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 修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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)); } }