reCAPTCHA protects your app against spam and bot. This package is tested with Laravel 5.5.
- PHP 7.0+
Captcha
is available via Composer:
$ composer require anam/captcha
Alternatively, add the dependency directly to your composer.json file:
"require": {
"anam/captcha": "~1.0"
}
Anam\Captcha
utilize the Laravel's package auto discovery feature. So, you don't need to add manually Service provider and Facade in Laravel application's config/app.php. Laravel will automatically register the service provider and facades for you.
Captcha comes with a Service provider and Facade for easy integration.
After you have installed the anam/captcha
, open the config/app.php
file which is included with Laravel and add the following lines.
In the $providers
array add the following service provider.
'Anam\Captcha\ServiceProvider\CaptchaServiceProvider'
Add the facade of this package to the $aliases
array.
'Captcha' => 'Anam\Captcha\Facade\Captcha'
You can now use this facade in place of instantiating the converter yourself in the following examples.
First, register keys for your site at https://www.google.com/recaptcha/admin
Add RECAPTCHA_SITE_KEY
and RECAPTCHA_SECRET
in .env
file :
RECAPTCHA_SITE_KEY=site_key
RECAPTCHA_SECRET=secret
By default, The package will try to load keys from environment. However, you can set them manually:
$captcha = new \Anam\Captcha\Captcha('recaptcha_secret');
Blade directives:
// reCAPTCHA v2
@captcha(site_key)
// Invisible reCAPTCHA
@invisiblecaptcha(site_key)
Just add @captcha()
blade directive to the form.
<form method="POST" action="/captcha" id="captcha-form">
{{ csrf_field() }}
<label>Name</label>
<input type="text" name="name">
<label>Your message</label>
<textarea name="message" rows="5"></textarea>
<br>
@captcha()
<br>
<input type="submit" value="Submit">
</form>
For more advanced integration, Please visit the following link: https://developers.google.com/recaptcha/docs/display
Add @invisiblecaptcha()
directive to the form where you want to appear the submit button. Please note, The @invisiblecaptcha
directive will inject the submit button for you. If you want to style the submit button, .g-recaptcha
class available for you.
<form method="POST" action="/captcha" id="captcha-form">
{{ csrf_field() }}
<label>Name</label>
<input type="text" name="name">
<label>Your message</label>
<textarea name="message" rows="5"></textarea>
<br>
@invisiblecaptcha()
</form>
Caveat: If view has more than one forms, the @invisiblecaptcha()
might not work as it will submit the first form. In these cases, you have to integrate the reCAPTCHA manually.
Please visit the following link: https://developers.google.com/recaptcha/docs/invisible
Handling the request:
use Anam\Captcha\Captcha;
use Illuminate\Http\Request;
class CaptchaController extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \Anam\Captcha\Captcha $captcha
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Captcha $captcha)
{
$response = $captcha->check($request);
if (! $response->isVerified()) {
dd($response->errors());
}
dd($response->hostname());
}
}
The MIT License (MIT). Please see LICENSE for more information.