laravel中使用mews/captcha包实现验证码

  • Simon 发布于 2018-05-02
  • 栏目: php
  • 7847人围观
  • 0评论


TIM截图20180502175502.jpg


packagist.org的地址: https://packagist.org/packages/mews/captcha


1.安装
composer require mews/captcha


2. 注册providers (laravel 5.5及以上可跳过此步骤)

在config/app.php的providers节点下追加

Mews\Captcha\CaptchaServiceProvider::class,


3. 注册aliases (laravel 5.5及以上可跳过此步骤)

在config/app.php的aliases节点下追加:

'Captcha' => Mews\Captcha\Facades\Captcha::class,


4. 生成配置文件

运行

php artisan vendor:publish


进入config/captcha.php 文件,修改default 数组 可以对验证码进行样式、数量、大小上的修改。


5.可用的方法
// 返回图片 (二选一)
captcha();
Captcha::create();

// 返回验证码地址 (二选一)
captcha_src();
Captcha::src();

// 返回img标签 (二选一)
captcha_img();
Captcha::img();

// 使用其他验证码配置 (二选一)
captcha_img('flat');
Captcha::img('inverse');


6.简单使用例子

前端页面

<img src="{{ captcha_src() }}" onclick="this.src='{{ captcha_src() }}'+Math.random()"/>


// 重写AuthController 登录验证方法,添加验证规则

/**

* @param Request $request

*/

protected function validateLogin(Request $request)

{

    $this->validate($request, [

        $this->username() => 'required|string',

        'password' => 'required|string',

        'captcha'  => 'required|captcha'

    ]);

}


7.添加中文释义

打开 resources/zh-CN/validation.php,在总数组中追加如下键值对

'captcha' => ':attribute 不正确。',

在 attributes 数组中追加如下键值对:

'captcha' => '验证码',


评论