• 监听事件 队列 邮件发送


    //调用的方法
    public function userAdd(){
    $data['username']='张三';
    $data['password']='123456';
    event(new SendEmailCodeEvent($data));
    $this->dispatch(new SendMySelfJob());
    }

    //先去生成一个队列的job 文件例如 php artisan make:job SendMailJob
    //邮件发送的队列
    <?php
    namespace AppJobs;

    use IlluminateBusQueueable;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateFoundationBusDispatchable;
    use IlluminateMailMessage;
    use IlluminateQueueInteractsWithQueue;
    use IlluminateQueueSerializesModels;
    use IlluminateSupportFacadesMail;

    class SendMySelfJob implements ShouldQueue
    {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
    * Create a new job instance.
    *
    * @return void
    */
    public function __construct()
    {
    //
    }

    /**
    * Execute the job.
    *
    * @return void
    */
    public function handle()
    {
    Mail::raw('邮件发送',function (Message $message){
    $message->to('*******@qq.com');
    });
    }
    }

    //监听者
    <?php

    namespace AppListeners;

    use AppEventsSendEmailCodeEvent;
    use AppModelsmodelsexamUser;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateQueueInteractsWithQueue;

    class SendEmailCodeListener
    {
    /**
    * Create the event listener.
    *
    * @return void
    */
    public function __construct()
    {
    //
    }

    /**
    * Handle the event.
    *
    * @param SendEmailCodeEvent $event
    * @return void
    */
    public function handle(SendEmailCodeEvent $event)
    {
    User::create($event->data);
    }
    }
    //发生的事件者
    <?php

    namespace AppEvents;

    use AppJobsSendMySelfJob;
    use IlluminateBroadcastingChannel;
    use IlluminateBroadcastingInteractsWithSockets;
    use IlluminateBroadcastingPresenceChannel;
    use IlluminateBroadcastingPrivateChannel;
    use IlluminateContractsBroadcastingShouldBroadcast;
    use IlluminateFoundationEventsDispatchable;
    use IlluminateQueueSerializesModels;

    class SendEmailCodeEvent
    {
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
    * Create a new event instance.
    *
    * @return void
    */
    public $data;
    public function __construct($data)
    {
    $this->data = $data;
    }

    /**
    * Get the channels the event should broadcast on.
    *
    * @return IlluminateBroadcastingChannel|array
    */
    public function broadcastOn()
    {
    return new PrivateChannel('channel-name');
    }
    }

    然后调用就可以了
    队列的任务要手动去执行














  • 相关阅读:
    第1周学习进度
    四则运算题1
    性能监控系统 | 从0到1 搭建Web性能监控系统
    数据库 | Oracle数据库查表空间使用情况
    性能测试 | 系统运行缓慢,CPU 100%,Full GC次数过多问题排查
    Markdown | 语法
    性能测试 | 理解单线程的Redis为何那么快?
    性能测试 | 理解分布式、高并发、多线程
    性能测试 | 常见的性能测试指标
    mysql 中varchar(50)最多能存多少个汉字
  • 原文地址:https://www.cnblogs.com/abcdefghi123/p/14541861.html
Copyright © 2020-2023  润新知