Use the Captcha extension package to generate a verification code for the Laravel5 application. 1. install
We use Composer to install the Captcha extension package:
composer require mews/captcha
Note: To use this extension package in Windows, you also need to install the GD2 extension (remove the comment before php_gd2.dll in php. ini ).
2. configuration
Before using the Captcha service provider, you must register the service provider in config/app. php:
'providers' => [ // ... Mews\Captcha\CaptchaServiceProvider::class,]
Register the corresponding facade at the same time:
'aliases' => [ // ... 'Captcha' => Mews\Captcha\Facades\Captcha::class,]
If you want to use custom configuration, you can also publish the configuration file to the config Directory:
$ php artisan vendor:publish
Edit the newly generated captcha. php:
return [ 'default' => [ 'length' => 5, 'width' => 120, 'height' => 36, 'quality' => 90, ], // ...];
3. example
// app/Http/routes.phpRoute::any('captcha-test', function(){ if (Request::getMethod() == 'POST') { $rules = ['captcha' => 'required|captcha']; $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { echo 'Incorrect!
'; } else { echo 'Matched :)
'; } } $form = ''; return $form;});
The display effect is as follows:
To return a native image, you can call this function:
captcha();
Or
Captcha::create();
If you want to return a URL:
captcha_src();
Or
Captcha::src();
If you want to return HTML:
captcha_img();
In this example, we use this function or call the method on the Captcha facade:
Captcha::img();
To use different configuration items in the configuration file captcha. php, you can call:
captcha_img('flat');Captcha::img('inverse');