


'Specifications' for developing SMS verification code sending function in Laravel (picture)
Laravel is a simple and elegant PHP Web development framework (PHP Web Framework). Next, through this article, I will share with you the "standard" development SMS Verification code sending function in Laravel. Friends who need it can refer to it
Laravel Introduction
Laravel is a simple and elegant PHP Web development framework (PHP Web Framework). It can free you from messy codes like noodles; it can help you build a perfect network APP, and every line of code can be concise and expressive.
Laravel already has an advanced PHP ActiveRecord implementation -- Eloquent ORM. It can easily apply "constraints" to both sides of the relationship, so that you have complete control over the data and enjoy all the conveniences of ActiveRecord. Eloquent natively supports all methods of the query builder (query-builder) in Fluent.
"Standard" development of SMS verification code sending function in LaravelRequirement scenarios
Send a "verification code" or "message notification" to your mobile phone or email.
Complete
First of all, the specification in Laravel is to use Laravel's "Message Notification", here based on the scenario is "Verification Code". This requirement is used by almost all software systems.
Create notification scene
The first step is to use php artisan make:notification to create a notification class. After the creation is successful, three notification classes already exist by default. There are three methods via, toMail and toArray. Because the verification code is sent, 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 to quickly create the data model and migration directly.
ThinkSNS+ migration is as follows:
The second step is to open the dataModel class and add the Illuminate\Notifications\Notifiable trait in it :
From the code, you can see that we have also added "soft deletion". Because it is sent based on the verification code based on the mobile phone number or email, there is no need for other built-in Huahuachangzi does not need to be recorded in the "Message Notification Data Table", so in the routeNotificationFor method we choose to directly return the account (mobile phone number or email) that needs to be sent.
Join Factory Mode, send quickly
Open database/factories/ModelFactory.php and add a notification about it Factory definition of data model:
In this way, we can quickly create and send verification codes through the factory(\Zhiyi\Plus\Models\VerificationCode::class) factory function notify.
Why add notification traits to the verification code data model?
First of all, Illuminate\Notifications\Notifiable is added to the User model by Laravel by default, so you can quickly send a notification to the user through $user->notify(), but there are Such a sentence:
Remember, you may use the Illuminate\Notifications\Notifiable trait on any of your models. You are not limited to only including it on your User model.
This is Laravel The original words of the official document mean that Illuminate\Notifications\Notifiable is not only used on the User model.
So we add Illuminate\Notifications\Notifiable in the verification code model, which is fully consistent with the correct use of Laravel notifications.
Develop notification class
First of all, there is a field channel in the data table migration, which is the notification channel identifier. We can use this value to Decide how to send the verification code, and this operation is implemented in the via of the notification class:
The way we choose is to directly return 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 some sending channels database, mail and nexmo
Complete email verification code sending
Actually, there are very few things we have to do in this step. When producing the notification class, the toMail method has been completed, so we can directly modify its message content.
Complete sending the SMS verification code
We use the overtrue/easy-sms package to send SMS, which is an SMS sending client developed by An Zhengchao. There are many built-in SMS platforms, and the implementation is also excellent. (Tucao: Although some details are problematic, such as not passing the gateway according to the contract calling method)
First rely on SMS to send the client package composer require overtrue/easy-sms and then create a new configuration/config/sms.php, what is the content? , just follow the instructions on the easy-sms homepage to add it. First post our configuration content (in order to reduce the number of words in the article, only keep the Alibaba configuration):
We have added a channel configuration for different scenarios, such as verification code scenario code to facilitate the messager to read the configuration.
Then open AppServiceProvider.php and add the following in register:
So far, the integration of EasySms in Laravel has been completed, but the actual function has not been developed yet. We Then look down.
Develop sms sending channel
Why develop? First of all, easy-sms supports a lot. You can consider developing a notification sending channel class for each sending platform separately, or you can only develop one SMS sending channel class. We choose to develop an SMS notification sending class through the easy-sms strategy. Mechanism to send verification codes to multiple platforms.
First, create a new app/Notifications/Channels/SmsChannel.php file. Because Laravel does not provide a generation function, you need to create this yourself. Just implement the send method. The content of SmsChannel is as follows:
The SMS notification sending channel based on easy-sms has been completed.
Development ScenarioSend Message
This part belongs entirely to easy-sms development, we create a new VerificationCodeMessage.php , the content is as follows:
Then we return to the VerificationCode verification code notification class and add the toSms method. My code is as follows:
Scenario configuration, such as templates for different channels of verification codes, etc., so that the message server can determine the configuration of the usage scenario based on the sending gateway.
Complain again, the contract design of easy-sms should also have this idea, but getContent/getTemplate/getData does not pass the gateway at all when the actual gateway is called. . .
Okay, our development is completed.
Send verification code
It has been added to the "factory" when creating the verification code data model, so we can use the factory directly The function is complete, send the demo:
#You’re done, easy-sms is a very good package.
The above is the detailed content of 'Specifications' for developing SMS verification code sending function in Laravel (picture). For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools