Note: If you do not have Composer yet, you can install it by following the instructions on https://getcomposer.org
composer require bonecms/laravel-captcha
{LARAVEL_ROOT}/config/app.php:
'providers' => [
...
LaravelCaptcha\Providers\LaravelCaptchaServiceProvider::class
],
It must be specified middleware "web" where the captcha validation. Since version 5.3 routes contains middleware "web" already. It defined by the provider "App\ProvidersRouteServiceProvider".
Generate a Captcha markup in your Controller:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use LaravelCaptcha\Facades\Captcha;
class MyController extends Controller
{
public function getExample()
{
return view('myView', ['captcha' => Captcha::html()]);
}
}
Showing a Captcha in a View:
...
{!! $captcha !!}
<input type="text" id="captcha" name="captcha">
...
Check user input during form submission:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use LaravelCaptcha\Facades\Captcha;
class MyController extends Controller
{
public function getExample()
{
return view('myView', ['captcha' => Captcha::html()]);
}
public function postExample(Request $request)
{
$this->validate($request, [
'captcha' => 'required|bone_captcha'
]);
// Validation passed
}
}
{LARAVEL_ROOT}/config/captcha.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Font
|--------------------------------------------------------------------------
| Supported: "DroidSerif".
|
*/
'font' => 'DroidSerif',
/*
|--------------------------------------------------------------------------
| Font size
|--------------------------------------------------------------------------
| Font size in pixels.
|
|
*/
'fontSize' => 26,
/*
|--------------------------------------------------------------------------
| Letter spacing
|--------------------------------------------------------------------------
| Spacing between letters in pixels.
|
*/
'letterSpacing' => 2,
/*
|--------------------------------------------------------------------------
| Code Length
|--------------------------------------------------------------------------
| You can specify an array or integer.
|
*/
'length' => [4, 5],
/*
|--------------------------------------------------------------------------
| Displayed chars
|--------------------------------------------------------------------------
| Enter the different characters.
|
*/
'chars' => 'QSFHTRPAJKLMZXCVBNabdefhxktyzj23456789',
/*
|--------------------------------------------------------------------------
| Image Size
|--------------------------------------------------------------------------
| Captcha image size can be controlled by setting the width
| and height properties.
|
|
*/
'width' => 180,
'height' => 50,
/*
|--------------------------------------------------------------------------
| Background Captcha
|--------------------------------------------------------------------------
| You can specify an array or string.
|
*/
'background' => 'f2f2f2',
/*
|--------------------------------------------------------------------------
| Colors characters
|--------------------------------------------------------------------------
| You can specify an array or string.
|
*/
'colors' => '2980b9',
/*
|--------------------------------------------------------------------------
| Scratches
|--------------------------------------------------------------------------
| The number of scratches displayed in the Captcha.
|
*/
'scratches' => 30,
/*
|--------------------------------------------------------------------------
| Captcha style
|--------------------------------------------------------------------------
| Supported: "wave".
|
*/
'style' => 'wave',
/*
|--------------------------------------------------------------------------
| Id of the Captcha code input textbox
|--------------------------------------------------------------------------
| After updating the Captcha focus will be set on an element with this id.
|
*/
'inputId' => 'captcha',
];