Runtime environment
- Mac system
- Mamp integrated environment + PHP5.6
- Laravel version 5.1.46
Send an activation email
- Create a user activation table, the user fills in the registration information, after the completion of the verification of registration information, after passing, the registration information is saved in the user table.
- Generate a unique token user to verify user activation information.
- Concatenate the token to the end of the user activation message and send the message to the user.
- Save the token value and user ID in the user activation table.
- After logging in to the mailbox, the user clicks the activation hyperlink to return to the registered site.
- Determine whether the user is successfully activated based on the parameters and activation status.
Specific implementation process:
1. Create a user activation table
php artisan make:model App\\Http\\Models\\Common\\UserActivation -m
Copy the code
After executing the above command, you can see that UserActivation files are created under app/Http/Models/Common and data migration files are created under data, as shown in the figure below:
At the end of the model file, there is a user method, which is used to find out the relationship. The user information can be queried according to the user_id in the user_Activations table.
Note that in the migration file above, I added a foreign key ‘user_id’ to the user activation table and then executed the data migration command:
php artisan migrate
Copy the code
Now that the user activation table is set up, let’s deal with the logic after the user registration information is filled in.
2. Process user registration information
After the user fills in the registration information, the information added by the user needs to be verified and can be written into the data table only after the verification passes. At present, it is assumed that the information submitted by the user can be written into the user table, so we need to modify the method of saving the user information, the code is as follows:
# # Http/App/Controllers/Admin/UserController PHP file/** * How to store user registration information */ publicfunction store(UserPostRequest $request) {
$data = $request->except('_token');
$data['password'] = Crypt::encrypt($data['password']);
$res = User::create($data);
if($res) {
$token = bcrypt( $res->email . time());
# Send email
Mail::send( 'admin.email.test'['user'= >$result.'token'= >$token].function ( $message ) use ( $result ) {
$subject = 'Ally Community Activation Mail';
$message->to( $result->email )->subject( $subject); }); // Save the activation information$addData = [
'token'= >$token.'user_id'= >$result->id,
];
$res = UserActivation::create($addData);
if ($res) {
return redirect( 'admin/user' );
} else {
return back()->with('errors'.'userActivation table data population failed! '); }}else {
return back()->with('errors'.'User table data populating failed! '); }}Copy the code
Now you can register a user and fill in the mailbox, you can see that an activation email has been sent to the registered mailbox, as shown in the picture below:
Of course, IN this case, I use laravel’s own API to send mail, and need to configure the mail configuration items first. For details, see laravel 5.1 using SMTP driver to send mail (including attachments and pictures) detailed tutorial
3. Configure the authentication route and add the authentication controller and method
Now you need to add the validation route to the route.php file:
Route::get('verify/account'.'Home\VerifyController@account');
Copy the code
Execute the command to create a controller on the command line:
php artisan make:controller Home/VerifyController --plain
Copy the code
Add the account method to the controller that validates the activation message:
# Http/App/Controllers/Home/VerifyController. PHP file/** * Verify the method of activating the message */ publicfunction account()
{
$token = request( 'verify' );
$res = UserActivation::where('token'.$token) - >where('active', 0) - >whereBetween('updated_at', [Carbon::now()->subDay(), Carbon::now()])->first();
if ($res) {
$res->update(['active'= > 1));return redirect('/login');
}
return redirect('/');
}
Copy the code
Now send activation emails and when registering users
Problems encountered while sending user activation messages
1. External variables cannot be used inside closure functions
When using the mail sending function, the external variables cannot be used because of the closure function. The previous code is as follows:
Mail::send('admin.email.send'['user'= >$result.'token'= >$token].function($message) {
$subject = 'Ally Community Activation Mail';
$message->to( $result->email )->subject($subject);
});
Copy the code
However, there is an error in the execution of this method, as shown in the figure:
After searching, I found the correct way to write it:
Mail::send('admin.email.send'['user'= >$result.'token'= >$token].function($message) use ($result) {
$subject = 'Ally Community Activation Mail';
$message->to( $result->email )->subject($subject);
});
Copy the code
Now you can see that PHPstorm also has no error messages and can also send email messages.
2. It takes a long time to send an email
After the registration information is submitted, the system will send an email to the mailbox of the registered user. The two processes are carried out synchronously, resulting in a long response time. Therefore, this problem needs to be solved (refer to the way of queue), and the problem of long response time will be dealt with in the next article.
conclusion
- The use keyword is used to introduce external variables when they are used within a closure function
- After the registration system is submitted, the server takes a long time to respond, so it is ready to process in queue mode.