laravel 使用mews/captcha 验证码

laravel version: 5.5.*

composer 安装

  • 安装需要的包
composer require mews/captcha
  • 配置: 在 config/app.php 中 找到 aliases数组加入以下代码
'aliases' => [
    'Captcha' => MewsCaptchaCaptchaServiceProvider::class,
]
  • 在命令行中执行以下命令, 生成 captcha.php 配置文件
 php artisan vendor:publish

基本使用

  • 模板中
<img src="{{captcha_src()}}">
  • 控制器中验证 captcha_check()
if(!captcha_check($request->input('code'))){
    return back()->withErrors("验证码有误");
}
  • 如果是前后端分离的项目
return response()->json([
  'src' => captcha_src()
]);

注:使用 src 作为验证码的图片地址即可,如果需要验证,可以使用验证器的 captcha 规则

配置验证码样式

如果你已经成功安装好了这个扩展包,你可以在 /config/captcha.php 中配置你想要的样式

7942449-7c06e62a5b20443c.png
custom theme

在使用的时候,加上配置的主题名称就好了...

captcha_src(‘custom’)
原文地址:https://www.cnblogs.com/liaohui5/p/10581601.html