Laravel验证码

Laravel验证码

composer 安装 安装需要的包

composer require mews/captcha

配置: 在 config/app.php 中 找到 aliases数组加入以下代码

'aliases' => [
    'Captcha' => Mews\Captcha\CaptchaServiceProvider::class,
]

在命令行中执行以下命令, 生成 captcha.php 配置文件

 php artisan vendor:publish

基本使用 设置验证码长度,在生成的captcha.php文件中修改默认长度即可 file 前端点击生成新的 file

模板中

<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 中配置你想要的样式

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

captcha_src(‘custom’)

原文链接:https://www.jianshu.com/p/588fecfe8dbd

猜你喜欢