Local environment:
System is: Mac Os
php version: 7.1
ThinkPHP version: 5.1.15
Recently I saw the think-queue extension class but the manual was not written. It was a headache. I found a lot of information and finally wrote a scenario. Sorry for the bad writing
First we create the project to download the tp framework and think-queue extension
Create the project:
Enter the project directory to view:
Download the extension class
After downloading, enter and check:
Then use MAMP to create a virtual
Create the database and data table after the machine;
create database if not exists myproject; use myproject; DROP TABLE IF EXISTS member; CREATE TABLE IF NOT EXISTS member( id int(11) AUTO_INCREMENT primary key comment 'ID', email VARCHAR(32) NOT NULL DEFAULT '' COMMENT '', password VARCHAR(255) NOT NULL default '' comment '', create_time INT(11) UNSIGNED NOT NULL DEFAULT 0 comment '', update_time INT(11) UNSIGNED NOT NULL DEFAULT 0 comment '', unique key (email) )engine innodb charset utf8;
Start the redis service
After that, some series of operations, such as opening Force routing, configure database, configure routing, configure template tags, etc....
Create page Register.php controller directory: application/index/controller/Register.php
Create Member.php model directory :application/index/model/Member.php
Create Register.php validator directory: application/index/validate/Register.php
Create the file sendActivationMail.php that processes the queue Directory: application/index/job/sendActivationMail .php
Create the operation method in the Register controller
<?php /** * User: 李昊天 * Date: 18/6/7 * Time: 上午3:15 * Email: haotian0607@gmail.com */ namespace app\index\controller; use think\Controller; use app\index\model\Member as MemberModel; use app\index\validate\Register as RegisterValidate; use think\Queue; class Register extends Controller { private $model = ''; public function initialize() { $this->model = new MemberModel(); } /** * 渲染模板 展示注册页面 * @return mixed */ public function index() { return $this->fetch('index'); } /** * 执行注册逻辑 */ public function doRegister() { if ($this->request->isPost()) { #实例化验证器 执行验证 如果验证失败跳转并且提示 $validate = new RegisterValidate(); $data = $this->request->post(); if (false === $validate->check($data)) return $this->error($validate->getError()); //此处应该加密密码 md5 sha1 hash 都可以 //写入注册的用户 $result = $this->model->allowField(['email', 'password'])->save($data); if ($result) { //注册完毕后获取到邮件账号 然后加入到队列 $this->sendActivationMail($this->model->email); return $this->success('注册成功,请前往邮箱激活您的账号!'); } else { return $this->error('注册失败'); } } } /** * @param string $email 邮箱账号 */ private function sendActivationMail($email = '') { $jobName = 'app\index\job\sendActivationMail'; //负责处理队列任务的类 $data = ['email' => $email]; //当前任务所需的业务数据 $jobQueueName = 'sendActivationMail'; //当前任务归属的队列名称,如果为新队列,会自动创建 $result = Queue::push($jobName, $data, $jobQueueName); if ($result) { echo date('Y-m-d H:i:s') . '一个新的队列任务'; } else { echo date('Y-m-d H:i:s') . '添加队列出错'; } // php think queue:work --queue sendActivationMail --daemon } }
sendActivationMail.php code
<?php /** * User: 李昊天 * Date: 18/6/7 * Time: 上午3:36 * Email: haotian0607@gmail.com */ namespace app\index\job; use think\queue\Job; use PHPMailer\Mail; use think\Exception; class sendActivationMail { /** * fire方法是消息队列默认调用的方法 * @param Job $job 当前的任务对象 * @param $data 发布任务时自定义的数据 */ public function fire(Job $job, $data) { //执行发送邮件 $isJobDone = $this->sendMail($data); //如果发送成功 就删除队列 if ($isJobDone) { print ("<warn>任务执行成功,,已经删除!" . "</warn>\n"); $job->delete(); } else { //如果执行到这里的话 说明队列执行失败 如果失败三次就删除该任务 否则重新执行 print ("<warn>任务执行失败!" . "</warn>\n"); if ($job->attempts() > 3) { print ("<warn>删除任务!" . "</warn>\n"); $job->delete(); } else { $job->release(); //重发任务 print ("<info>重新执行!第" . $job->attempts() . "次重新执行!</info>\n"); } } } /** * 发送邮件 * @param $data * @return bool */ private function sendMail($data) { $title = '账号激活邮件'; $msg = '欢迎您注册xxx网站,您的请点击一下连接激活您的账号!....'; try { return Mail::send($title, $msg, $data['email']); } catch (Exception $e) { return false; } } }
After writing the code, switch the controller to the current directory for execution
php think queue:work --queue sendActivationMail --daemon
There are very detailed comments in the code, but this is not complete,,, there is no error callback,
I will write the rest of the code when I have time next time!