"Specification" for developing SMS verification code sending function in Laravel

Source: Internet
Author: User
Tags php web development
Laravel is a simple, elegant PHP Web development Framework (PHP Web framework). Next through this article to share in the Laravel "specifications" of the development of SMS verification code to send the function, need to refer to the friend

Laravel Introduction

Laravel is a simple, elegant PHP Web development Framework (PHP Web framework). It frees you from the messy code of noodles, and it helps you build a perfect web app, and every line of code can be concise and expressive.

A set of advanced PHP ActiveRecord implementations-eloquent ORM-is already in laravel. It makes it easy to apply "constraints" to both sides of the relationship, so you have full control over the data and enjoy all the conveniences of ActiveRecord. Eloquent native supports all methods of the query constructor (Query-builder) in fluent.

development of "specification" in Laravel SMS verification code sending function Demand Scenarios

Send a "Verification code" or "message notification" that can be sent to your phone or mailbox.

Complete

First, the specification in Laravel is to use the Laravel "message notification", which is based on the scenario as "Captcha". This requirement is used by almost all software systems.

Create a notification scenario

The first step is to create a notification class using PHP artisan Make:notification, which already has three methods via, Tomail, and ToArray after the successful creation, because the verification code is sent and the 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 to quickly create a data model and migration.

The migration of thinksns+ is as follows:

The second step is to open the data model class and add illuminate\notifications\notifiable traits inside it:

From the code, we can see that we have also added "soft delete", because it is based on the phone number or email verification code sent, so no other built-in Huahuachangzi, and do not need to be recorded in the "Message Notification data table", so routenotificationfor Method we choose to return directly to the account (mobile number or email) that needs to be sent.

Add Factory mode, send fast

Open database/factories/modelfactory.php. Add a factory definition for the notification data model:

This allows us to quickly create a verification code and send a notification via the factory (\zhiyi\plus\models\verificationcode::class) factory function.

Why is the notification trait added in the CAPTCHA data model?

First illuminate\notifications\notifiable This trait, Laravel is added to the user model by default, so you can quickly send a notification to the user by $user->notify () But there is one sentence in the spec document:

Remember, the illuminate\notifications\notifiable trait on any of the your models. You is not limited to only including it on your User model.

This is Laravel official document, meaning that illuminate\notifications\notifiable is not just used on the User model.

So we added illuminate\notifications\notifiable in the CAPTCHA model to be fully compliant with Laravel notifications.

Development notification Classes

First, there is a field in the data table migration channel is the notification channel identification, we can determine by this value in what way to send the verification code, and this operation is implemented in the notification class via:

The way we choose is to directly return the channel value, this value can be any value, as long as we implement this notification channel, can be sent, while Laravel has built-in and some send channel database, mail and Nexmo

Complete message Verification code sent

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

Complete SMS Verification Code send

SMS send us using overtrue/easy-sms package, which is an ultra-development of an SMS sending client, has built up a lot of SMS platform, the implementation is also very good. (Spit slot: Although some details are problematic, such as not passing the gateway in accordance with the contract call method)

First rely on SMS send client package composer require overtrue/easy-sms and then create a new configuration/config/sms.php, content, just follow the Easy-sms home page instructions to add, first paste out our configuration content (in order to reduce the number of article words , only keep Ali greater than configuration):

We have added a channel configuration for different scenarios, such as CAPTCHA scene code to facilitate message reader configuration.

Then open the appserviceprovider.php in register to add the following:

At this point the integration of easysms in Laravel has been completed, but the actual functionality has not yet been developed, and we continue to look down.

Developing SMS Send Channels

Why develop? First of all, easy-sms support a lot, you can consider a separate for each sending platform to develop a notification Send channel class, can also be used to develop only one SMS Send channel class, we choose to develop an SMS notification sending class, through the EASY-SMS strategy mechanism to send verification code to multi-platform.

First, create a new app/notifications/channels/smschannel.php file because Laravel does not provide a build function, which needs to be created by itself, as long as the Send method is implemented. Smschannel content is as follows:

This easy-sms-based SMS notification delivery channel has been completed.

Development Scenario Send Message

This part belongs entirely to easy-sms use development, we create a new verificationcodemessage.php, the content is as follows:

Then we go back to the Verificationcode Captcha notification class, add the Tosms method, my code is as follows:

Scenario configuration, such as the template for different channels of verification code, so that the message can be based on the sending gateway to determine the configuration of the use of the scene.

Again, easy-sms the contract design should be the idea, but getcontent/gettemplate/getdata in the actual gateway call when there is no delivery gateway to come ...

Well, our development is complete.

Send Verification Code

It was added to the "factory" when creating the CAPTCHA data model, so we can use the factory function directly and send a demo:

Done, easy-sms is a very nice package yo.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.