Laravel默认是没有提供验证码组件的,如果使用验证码需要自行安装:
推荐使用
mews/captcha
专门为laravel设计开发的拓展包。
1、composer引入安装
{ "require": { "mews/captcha": "~2.0" } }
或者使用下面的命令: composer require mews/captcha Update your packages with ```composer update``` or install with ```composer install```.
'providers' => [ // ... 'Mews\Captcha\CaptchaServiceProvider', ]
In Windows, you'll need to include the GD2 DLL `php_gd2.dll` in php.ini.
And you also need include `php_fileinfo.dll` and `php_mbstring.dll` to fit the requirements of `mews/captcha`'s dependencies.
配置所在目录:
config/captcha.php
2、example Usage
Route::any('captcha-test', function() { if (request()->getMethod() == 'POST') { $rules = ['captcha' => 'required|captcha']; $validator = validator()->make(request()->all(), $rules); if ($validator->fails()) { echo '<p style="color: #ff0000;">Incorrect!</p>'; } else { echo '<p style="color: #00ff30;">Matched :)</p>'; } } $form = '<form method="post" action="captcha-test">'; $form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">'; $form .= '<p>' . captcha_img() . '</p>'; $form .= '<p><input type="text" name="captcha"></p>'; $form .= '<p><button type="submit" name="check">Check</button></p>'; $form .= '</form>'; return $form; });
3、使用示例
captcha(); //Return Image Captcha::create(); //Return Image captcha_src(); //Return URL Captcha::src(); //Return URL captcha_img(); //Return HTML Captcha::img(); //Return HTML Captcha::img('inverse'); //类型 captcha_img('flat');