Simple Google ReCaptcha package for Laravel 5
PHP 7.1 or grater
Are you still using PHP 5.6? Please go to V2
You can install the package via composer:
composer require biscolab/laravel-recaptcha:^3.0
The service provider must be registered in config/app.php
:
'providers' => [
...
Biscolab\ReCaptcha\ReCaptchaServiceProvider::class,
];
You can use the facade for shorter code. Add "ReCaptcha" to your aliases:
'aliases' => [
...
'ReCaptcha' => Biscolab\ReCaptcha\Facades\ReCaptcha::class,
];
Create config/recaptcha.php
configuration file using:
php artisan vendor:publish --provider="Biscolab\ReCaptcha\ReCaptchaServiceProvider"
Open config/recaptcha.php
configuration file and set api_site_key
, api_secret_key
and version
:
return [
'api_site_key' => 'YOUR_API_SITE_KEY',
'api_secret_key' => 'YOUR_API_SECRET_KEY',
'version' => 'v2' // supported: v2|invisible
'skip_ip' => [] // array of IP addresses - String: dotted quad format e.g.: 127.0.0.1
];
For more invermation about Site Key and Secret Key please visit Google RaCaptcha developer documentation Get more info about ReCAPTCHA version at https://developers.google.com/recaptcha/docs/versions skip_ip is a list of IP addresses that, if recognized, disable the recaptcha validation (return always true).
If you are migrating from an older version add skip_ip
array in recaptcha.php
configuration file.
Before starting please add validation recaptcha message to resources/lang/[LANG]/validation.php
file
return [
...
'recaptcha' => 'Hey!!! :attribute is wrong!',
];
Insert htmlScriptTagJsApi($formId)
helper before closing </head>
tag
You can also use ReCaptcha::htmlScriptTagJsApi($formId)
.
$formId
is required only if you are using ReCAPTCHA INVISIBLE
<!DOCTYPE html>
<html>
<head>
...
{!! htmlScriptTagJsApi(/* $formId - INVISIBLE version only */) !!}
or
{!! ReCaptcha::htmlScriptTagJsApi(/* $formId - INVISIBLE version only */) !!}
</head>
After you have to insert htmlFormSnippet()
helper inside the form where you want to use the field g-recaptcha-response
You can also use ReCaptcha::htmlFormSnippet()
.
<form>
...
{!! htmlFormSnippet() !!}
<input type="submit">
</form>
After you have to insert htmlFormButton($buttonInnerHTML)
helper inside the form where you want to use ReCAPTCHA. This function creates submit button therefore you don't have to insert or similar.
You can also use ReCaptcha::htmlFormButton($buttonInnerHTML)
.
$buttonInnerHTML
is what you want to write on the submit button
<form>
...
{!! htmlFormButton(/* $buttonInnerHTML - Optional */) !!}
</form>
Add recaptcha to your rules
$v = Validator::make(request()->all(), [
...
'g-recaptcha-response' => 'recaptcha',
]);
Print form errors
$errors = $v->errors();
composer test