Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop CAPTCHA sending function of "specification" in Laravel

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

What is ThinkSNS?

ThinkSNS (TS for short), a platform-wide comprehensive social system, provides social software development and technical solutions for large, small and medium-sized enterprises and entrepreneurs at home and abroad.

Demand scenario

Send "CAPTCHA" or "message notification" to your mobile phone or mailbox.

Complete

First of all, the specification in Laravel is to use Laravel's "message notification", which is "CAPTCHA" based on the scenario. This requirement is used in almost all software systems.

Create a notification scenario

The first step is to create a notification class using php artisan make:notification. After successful creation, three methods, via, toMail and toArray, already exist by default. Because the CAPTCHA is sent, the control class is named VerificationCode.

Then create a CAPTCHA data model and data table migration, which can be created directly and quickly using php artisan make:model "VerificationCode"-m.

The migration of ThinkSNS+ is as follows:

The second step is to open the data model class and add Illuminate\ Notifications\ Notifiable characters to it:

From the code, we can see that we have also added "soft deletion". Because it is sent based on the verification code of the mobile phone number or mailbox, we do not need other built-in fancy intestines or record it in the "message Notification data Table", so we choose to directly return the account number (mobile number or mailbox) that needs to be sent in the routeNotificationFor method.

Join factory mode and send it quickly #

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

In this way, we can quickly create CAPTCHA and send notifications through the factory (\ Zhiyi\ Plus\ Models\ VerificationCode::class) factory function.

Why are notification traits added to the CAPTCHA data model?

First of all, the character Illuminate\ Notifications\ Notifiable is added to the User model by default, so you can quickly send a notification to the user through $user- > notify (), but there is a sentence in the specification document:

Remember, you may use theIlluminate\ Notifications\ Notifiabletrait on any of your models. You are not limited to only including it on yourUsermodel.

This is the original word of the Laravel official document, meaning that Illuminate\ Notifications\ Notifiable is not just used on the User model.

So we add Illuminate\ Notifications\ Notifiable to the CAPTCHA model in full compliance with the correct use of Laravel notifications.

Development notification class

First of all, there is a field channel in the data table migration, that is, the notification channel ID. We can decide how to send the verification code based on this value, and this operation is implemented in the via of the notification class:

Our choice is to directly return the channel value, which can be any value, as long as we implement the notification channel, it can be sent, and Laravel has built-in and some sending channels database, mail and nexmo

Complete the sending of email verification code

In fact, there is very little we need to do in this step. when we produce the notification class, we have already completed the toMail method, so we can modify the message content directly.

Complete the sending of SMS verification code

We use overtrue/easy-sms package to send short messages, which is a short message sending client developed by an Zhengchao. There are many SMS platforms built in, and the implementation is also very excellent. (complaint: although there are problems with some details, such as not invoking the method delivery gateway in accordance with the contract)

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

We have added a channel configuration for different scenarios, such as CAPTCHA scenario code to facilitate the message server to read the configuration.

Then open AppServiceProvider.php and add the following to register:

At this point, the integration of EasySms in Laravel is complete, but the actual functionality has not yet been developed, so let's move on.

Develop sms transmission channel

Why should it be developed? First of all, easy-sms supports a lot. We can consider developing a notification sending channel class for each sending platform, or we can develop only one sms sending channel class. We choose to develop a sms notification sending class to send CAPTCHA to multiple platforms through the policy mechanism of easy-sms.

First of all, create a new app/Notifications/Channels/SmsChannel.php file, because Laravel does not provide a generating function, this needs to be created by yourself, as long as you implement the send method. The SmsChannel content is as follows:

In this way, the easy-sms-based SMS notification sending channel is completed.

Send messages in the development scenario

This part belongs entirely to easy-sms usage development. Let's create a new VerificationCodeMessage.php with the following contents:

Then go back to the VerificationCode CAPTCHA notification class and add the toSms method as follows:

As you can see, what's the use of passing a config in when you instantiate the CAPTCHA message? In fact, it has been mentioned earlier: add the configuration of long scenarios to the configuration file, such as the template of different channels of CAPTCHA, so that the messenger can determine what the configuration of the usage scenario is based on the sending gateway.

Complain again that the contract design of easy-sms should be the same idea, but getContent/getTemplate/getData does not pass the gateway at all when the actual gateway is called.

All right, our development is complete.

Send verification code

It was added to the "factory" when the CAPTCHA data model was created, so we can use the factory function directly to send the demonstration:

It's done. Easy-sms is a great bag.

The above codes are all from ThinkSNS Plus. For the complete development code, you can see the warehouse:

GitHub: https://github.com/slimkit/thinksns-plus (it is not easy to open source, seek Star)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report