您现在的位置是:网站首页>PHPPHP
Laravel5+登录验证码 使用captcha方法
左鹏07-18 17:06:21【PHP】2671人已围观
简介一、安装captchacomposer require mews/captcha二、配置[/config/app.php]添加以下配置'providers' => [ // ... Mews\Captcha\CaptchaServiceProvider::class, ] 'aliases
一、安装captcha
composer require mews/captcha
二、配置[/config/app.php]添加以下配置
'providers' => [ // ... Mews\Captcha\CaptchaServiceProvider::class, ] 'aliases' => [ // ... 'Captcha' => Mews\Captcha\Facades\Captcha::class, ]
三、生成自定义配置文件
php artisan vendor:publish
运行之后,就可以在 config/captcha.php 中进行配置了。
注:如果运行后提示输入框请参考这个教程[Laravel执行php artisan vendor:publish卡住问题解决方法]
四、使用 Captcha 为 auth 组件添加验证码功能
1、在登录视图中增加验证码的选项,可以加到密码和 remember me 之间
/resources/views/auth/login.blade.php<div class="form-group"> <label for="captcha" class="col-md-4 control-label">验证码</label> <div class="form-group"> <div class="col-md-3"> <input id="captcha" class="form-control" type="captcha" name="captcha" value="{{ old('captcha') }}" required> @if($errors->has('captcha')) <div class="col-md-12"> <p class="text-danger text-left"><strong>{{$errors->first('captcha')}}</strong></p> </div> @endif </div> <div class="col-md-4"> <img src="{{captcha_src()}}" style="cursor: pointer" onclick="this.src='{{captcha_src()}}'+Math.random()"> </div> </div> </div>
2、重写AuthController 登录验证方法,并自定义提示信息: 修改 App\Http\Controllers\Auth\LoginController
#首先要引入如下代码:use Illuminate\Http\Request; #重写validateLogin方法: #在验证里面加入验证码的规则验证即可/** * DESC: 重写 AuthenticatesUsers 登录验证方法,并自定义提示信息; * 原验证方法 Illuminate\Foundation\Auth\AuthenticatesUsers * @param Request $request */ protected function validateLogin(Request $request){ $this->validate($request, [ $this->username() => 'required|string', 'password' => 'required|string' 'captcha' => 'required|captcha', ],[ 'captcha.required' => '请填写验证码', 'captcha.captcha' => '验证码错误', ]); }
完成后,刷新登录页面即可完成安装了
点击排行

本栏推荐

猜你喜欢
站点信息
- 建站时间:2018-09-18
- 网站程序:Laravel
- 主题模板:《今夕何夕》
- 文章统计:85条
- 统计数据:
- 微信公众号:扫描二维码,关注我们
