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;
            }
相关推荐
亿是守候 & 亿是承诺6 分钟前
第二章 控制系统的数学模型
android
Carson带你学Android31 分钟前
ADK for Kotlin:Google 官方 AI Agent 教程来了
android·agent·ai编程
一航jason12 小时前
Android平台推理框架及试用场景模型对比
android·人工智能·ai·架构·ai编程·llama
一航jason12 小时前
Android 端侧大模型推理框架对比
android·人工智能·ai·ai编程·llama·ai-native
新时代牛马13 小时前
cyclictest 毛刺从哪来?从ftrace、latencytop、perf到IRQ/调度延迟定位
android·开发语言·python·kotlin
光电的一只菜鸡14 小时前
为什么调整LSC会对AF产生影响
android·开发语言·kotlin
凛_Lin~~15 小时前
LiveData 源码解析
android·安卓·livedata
hai_android15 小时前
FusibleFlow:Kotlin Flow 的融合机制原理
android·kotlin
龚寿生15 小时前
23-敏捷开发实战:从Scrum到看板的团队协作方法论
android·学习·scrum·敏捷流程
zhangguojia717 小时前
Activity启动流程(四):从setContentView到View树创建与Window挂载
android