Using laravel sms to Construct SMS Verification Code Sending Verification Function

  • 2021-08-16 23:15:47
  • OfStack

laravel realizes SMS verification code function, and there are two popular packages found in searching data:

One is laravel sms Address https://github.com/toplan/laravel-sms

One is easy sms Address https://github.com/overtrue/easy-sms,

The project needs to realize a function of sending and verifying SMS verification code. The previous method was slightly cumbersome. After being instructed by experts, it is found that laravel-sms can be used instead. And the configuration and use are easy to learn. So there is this example.

This example uses Laravel 5.5, Api Starter Kit, and Laravel Sms 2.6.

The short message service provider used in this example is cloud film.

Installation

Execute under the project root (recommended):


composer require toplan/laravel-sms:~2.6
composer require toplan/laravel-sms:~2.6 

You can also add in the require field of composer. json:


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 

Then execute it under the root directory of the project:


composer update
composer update

In the providers array of config/app. php, add:


Toplan\PhpSms\PhpSmsServiceProvider::class,
Toplan\Sms\SmsManagerServiceProvider::class,
Toplan\PhpSms\PhpSmsServiceProvider::class,
Toplan\Sms\SmsManagerServiceProvider::class, 

And add to the aliases array:


'PhpSms' => Toplan\PhpSms\Facades\Sms::class,
'SmsManager' => Toplan\Sms\Facades\SmsManager::class,
'PhpSms' => Toplan\PhpSms\Facades\Sms::class,
'SmsManager' => Toplan\Sms\Facades\SmsManager::class,

Execute under the project root directory:


php artisan vendor:publish --provider="Toplan\PhpSms\PhpSmsServiceProvider"
php artisan vendor:publish --provider="Toplan\Sms\SmsManagerServiceProvider"
php artisan vendor:publish --provider="Toplan\PhpSms\PhpSmsServiceProvider"
php artisan vendor:publish --provider="Toplan\Sms\SmsManagerServiceProvider" 

Two configuration files are generated in the config folder: phpsms. php and laravel-sms. php.

Agent information and balanced scheduling scheme can be configured in phpsms. php.

In laravel-sms. php, you can configure the sending and verification scheme of verification code.

The 2015_12_21_111514_create_sms_table. php file is also copied to database\ migrations. Used to generate the laravel_sms table.

Configure

Here, just take the cloud film as an example.

Configuring phpsms. php

Set the agent information of the cloud slice in the agnets array in phpsms. php.


'YunPian' => [
 // User only 1 Identification, must 
 'apikey' => ' Fill in your  APIKEY',
],
'YunPian' => [
 // User only 1 Identification, must 
 'apikey' => ' Fill in your  APIKEY',
],

Set the scheme array and configure the balanced scheduling scheme.


'scheme' => [
 'YunPian',
],
'scheme' => [
 'YunPian',
], 

Configuring laravel-sms. php

Set up built-in routes.


'route' => [
 'enable'  => true,
 'prefix'  => 'laravel-sms', 
 'middleware' => ['api'],
],
'route' => [
 'enable'  => true,
 'prefix'  => 'laravel-sms', 
 'middleware' => ['api'],
],

Set the request interval in seconds.


'interval' => 60,
'interval' => 60, 

Set number verification rules.


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
0

Set verification code rules.


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
1

Set the verification code content SMS.


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
2

If necessary, you can open the database log. You need to run php artisan migrate ahead of time to generate the laravel_sms table.


'dbLogs' => 'ture',
'dbLogs' => 'ture',

API Implementation

SmsCodeUtil. php is created under app/Utils, and the verification code sending and verification functions are realized in it. In this way, other classes can be called at any time, which improves the reusability of code.

Transmitting module

The mobile phone number needs to be verified before sending, including:


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
4

After verification, use requestVerifySms () to send the verification code.

The specific code is as follows:


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
5

Verification module

When logging in, you may need to verify your mobile phone number and verification code. Therefore, it is necessary to add verification code verification function in SmsCodeUtil. php. Here, the code has been given on the official Github, and it can be modified slightly.


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
6

Functional testing

Next, configure the routing and controller to test whether the function is normal.

Can be opened at the same time host-domain/laravel-sms/info Check the sending and verification status of verification code SMS.

If database logging is enabled, you can view the details of SMS sending results in the laravel_sms table.

First add in api. php:


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
7

Add in LoginController. php:


use App\Utils\SmsCodeUtil;
class LoginController extends Controller {
 use SmsCodeUtil;
 ...
}
use App\Utils\SmsCodeUtil;
class LoginController extends Controller {
 use SmsCodeUtil;
 
 ...
}

Then use Postman or other similar tools to test Api functionality.

Send verification code


"toplan/laravel-sms": "2.6"
"toplan/laravel-sms": "2.6" 
9

If verified and sent successfully, it will return:


{
  "message": " SMS verification code was sent successfully, please pay attention to check it ",
  "status_code": 200
}
{
  "message": " SMS verification code was sent successfully, please pay attention to check it ",
  "status_code": 200
}

The mobile phone number filled in at the same time receives the verification code.

If validation fails or sending fails, the corresponding error message will be returned.

Verification code


POST  Server address /api/auth/validate-sms-code
{
  "phone_number": " Mobile phone number ",
  "sms_code": " Verification code "
}

POST  Server address /api/auth/validate-sms-code
{
  "phone_number": " Mobile phone number ",
  "sms_code": " Verification code "
}

If validation is passed, nothing is returned.

If the validation fails, the corresponding error message will be returned.

Localized prompt information language

Customization of some of the prompt information is provided in laravel-sms. php. If you want to convert the rest of the prompt information to the local language, you need to deal with it separately.

First make sure that the language settings in config/app. php are correct. This is set to zh_cn.


'locale' => 'zh_cn',
'locale' => 'zh_cn', 

Then create a new validation. php under the resources\ lang\ zh_cn folder and fill in the localization information:


composer update
composer update
3

If you re-examine the relevant address of POST, you can see that the corresponding prompt information language has been localized.

Summarize


Related articles: