laravel5.8中实现验证码组件的安装和验证

本篇文章主要讲解使用laravel5.8自带的验证码库实现验证码验证的效果教程。通过本教程你可以快速接入到自己的项目中开发相应的验证功能。

作者:任聪聪 (rccblogs.com)

日期:2023年12月17日

实际效果

安装步骤

步骤一、输入命令

bash 复制代码
composer require mews/captcha

步骤二、配置 config/app.php文件内容

1.providers部分

bash 复制代码
        Mews\Captcha\CaptchaServiceProvider::class,

2.aliases 部分

shell 复制代码
        'Captcha' => Mews\Captcha\Facades\Captcha::class,

步骤三、发布

shell 复制代码
php artisan vendor:publish

弹出下面信息时,依据自己对应的编号继续输入即可

这里我输入的是数字:9

发布成功提示:

shell 复制代码
Copied File [\vendor\mews\captcha\config\captcha.php] To [\config\captcha.php]
Publishing complete.

E:\develop\php\51powand>

使用验证码

一、配置验证码信息

在新的验证码的配置文件中。

二、页面中调用

shell 复制代码
                            <img src="{{captcha_src()}}" style="cursor: pointer" onclick="this.src='{{captcha_src()}}'+Math.random()" >

三、方法中进行验证

shell 复制代码
			use Illuminate\Support\Facades\Validator;
			
			
			
			$messages = [
                'verificationCode.required' => '验证码不可为空',
                'verificationCode.captcha' => '验证码错误',
            ];
			
            $validator = Validator::make($request->all(), [
                'verificationCode' => 'required|captcha',
            ], $messages);
			
            if ($validator->fails()) {
                $errorMessage = $validator->errors()->first();
                return $errorMessage;
            }
相关推荐
_阿南_3 小时前
Android文件读写和分享总结
android
通玄12 小时前
Jetpack Compose 入门系列(六):Navigation 3 页面导航
android
rocpp14 小时前
Android 多语言切换实战:从 Context 到 Android 13 应用语言适配
android·kotlin
释然小师弟15 小时前
Android开发十年:反思与回顾
android·后端·嵌入式
黄林晴17 小时前
用了这么久 Koin Scope,原来一直都用错了?
android·kotlin
爱勇宝1 天前
我做了一个只用来搜歌词的小 App
android·前端·后端
众少成多积小致巨1 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
Coffeeee2 天前
如何使用Glide和Coil加载WebP动图
android·kotlin·glide
Kapaseker2 天前
5 分钟搞懂 Kotlin DSL
android·kotlin
恋猫de小郭2 天前
AI Agent 开发究竟是啥?如何用 AI 开发 Agent ?深入浅出给你一套概念
android·前端·ai编程