菜单

In Laravel, “standard” development of SMS verification code sending function

2017年11月22日 - PHPer

Brief introduction of Laravel

Laravel is a concise and elegant PHP Web development framework (PHP Web Framework). It frees you from messy code like noodles; it helps you build a perfect network of APP, and each line of code can be concise and expressive.

In the Laravel already has a set of advanced PHP ActiveRecord implementation – Eloquent

ORM. It can easily apply “constraints” to the two sides of the relationship, so you have complete control of the data, and enjoy all the convenience of ActiveRecord. Eloquent natively supports all methods of query constructor (query-builder) in Fluent.

Laravel in the “norms” of the development of SMS verification code sent function demand scenarios

Send the “verification code” or “message notification” to the mobile phone or mailbox.

complete

First of all, the specification in Laravel is to use Laravel’s “message notification”, which is based on the scene as “verification code”. This requirement is available to almost all software systems.

Create notification scenarios

The first step is to create a notification class using PHP artisan make:notification. After creating the success, there are already three methods via, toMail and toArray by default, because it is sending authentication code, and this control class is named VerificationCode.

Then create a verification code data model and data table migration, you can use PHP artisan make:model “VerificationCode” -m directly and quickly create data model and migration.

The migration of ThinkSNS+ is as follows:

The second step is to open the data model class and add the IlluminateNotificationsNotifiable character inside it:

We can see from the code inside, also added a “soft delete”, because it is a mobile phone number or email verification code sent on, so there is no need to other built-in huahuachangzi, also do not need to record “message data table”, so the routeNotificationFor method we choose to return directly to send account (mobile phone number or email).

Join factory mode, send quickly

Open the database/factories/ModelFactory.php and add a factory definition to the notification data model inside:

In this way, we can quickly create a verification code and send a notification through the factory function of factory (ZhiyiPlusModelsVerificationCode:: class).

Why does the data model increase the notification characteristics in the validation code?

First of all, the IlluminateNotificationsNotifiable character, Laravel defaults to the User model, so through $user-> notify () can quickly send a notice to the user, but there is such a sentence in the specification document:

Remember, you may use the IlluminateNotificationsNotifiable trait

On any of your models. You are not your limited to only including it on

User model.

This is the official document of Laravel, which means that IlluminateNotificationsNotifiable is not just used on the User model.

So we add IlluminateNotificationsNotifiable in the code model to fully comply with the correct use of the Laravel notification.

Development notification class

First of all, there is a field channel in the data table migration, that is, the notification channel identifier. We can decide which way to send the authentication code according to this value, and this operation is implemented in the via of the notification class:

The way we choose is to go back directly to the channel value, which can be any value. As long as we implement this notification channel, we can send it, and Laravel has built-in and some sending channels database, mail and nexmo

Complete mail verification code sending

In fact, this step we have to do little, the production notification class, has completed the toMail method, so we directly modify the message content can be.

Complete SMS verification code sending

Short message sending we use overtrue/easy-sms package, this is a text messaging client developed by An Zhengchao, has built a lot of SMS platform, the implementation is also very excellent. (Tucao: Although there are some details, such as not transferring the gateway in accordance with the contract call method)

First of all depends on the SMS client package composer require overtrue/easy-sms and the new /config/sms.php configuration, content, in accordance with the easy-sms page that can be increased, the content of our first posted configuration (in order to reduce the number of words, only more than Ali configuration):

We have added a channel configuration for different scenarios, such as the authentication code scenario code to facilitate message reader configuration.

Then open AppServiceProvider.php and add as below in register:

So far, the integration of EasySms in Laravel has been completed, but the actual function hasn’t been developed yet, and we’ll go on to see it.

Developing SMS sending channels

Why should we develop? First of all, a lot of easy-sms support, can be considered separately for the development of a class of each notification channel transmitting platform, can also be used only to develop a SMS transmission channel, we chose to develop a SMS notification class, through the mechanism of easy-sms multi platform strategy to send verification code.

First, build a new app/Notifications/Channels/SmsChannel.php file, because Laravel does not provide the generating function, and this needs to be created by itself, as long as the send method is implemented. SmsChannel reads as follows:

So easy-sms based SMS notification sending channel has been completed.

Sending messages from development scenarios

This part is completely easy-sms development, we build a new VerificationCodeMessage.php, the contents are as follows:

Then we go back to the VerificationCode code notification class, adding the toSms method, and my code is as follows:

Scene configuration, such as verification code, template of different channels, etc., so that the message can be judged according to the sending gateway configuration of the use of the scene.

Again Tucao, easy-sms contract design should be this idea, but getContent/getTemplate/getData in the actual gateway call, there is no gateway to pass…

All right, we’re done.

Send authentication code

When you create the validation code data model, it has been added to the factory, so we can use the factory function directly and send the demo:

Easy-sms is a great bag.

The above code comes from the ThinkSNS Plus, see the complete development code can look at the warehouse:

GitHub: https://github.com/slimkit/thinksns-plus

发表评论

电子邮件地址不会被公开。