Laravel Captcha
Laravel Captcha is a wrapper around HCaptcha & Google Recaptcha. It provides very easy-to-use Facade, Validation Rule, and blade directives.
Installation
Requires PHP 7.3+
- Install via Composer:
composer require rahul900day/laravel-captcha
- Publish the config file with:
php artisan vendor:publish --tag="captcha-config"
- Add required configuration to
.env
file:
CAPTCHA_DRIVER=hCaptcha
CAPTCHA_SITE_KEY="{Your Site Key}"
CAPTCHA_SECRET_KEY="{Your Site Secret}"
Don't know how to get the Site Key or Site Secret ?
Read HCaptcha or ReCaptcha Docs.
Configuration
Laravel Captcha supports very different customizations like theme, language, size. Just add these configurations to your .env
file to customize.
Supported ENV Variables
Variable | Default | Description |
---|---|---|
CAPTCHA_DRIVER | hCaptcha |
Default Captcha driver Supported: hCaptcha , reCaptcha |
CAPTCHA_SITE_KEY | '' |
The Site Key. HCaptcha Docs: https://docs.hcaptcha.com/configuration ReCaptcha Docs: https://developers.google.com/recaptcha/docs/display |
CAPTCHA_SECRET_KEY | '' |
The Site Secret. HCaptcha Docs: https://docs.hcaptcha.com/configuration ReCaptcha Docs: https://developers.google.com/recaptcha/docs/display |
CAPTCHA_LOCALE | en |
The Captcha localization code. HCaptcha Supported Codes: https://docs.hcaptcha.com/languages Recaptcha Supported Codes: https://developers.google.com/recaptcha/docs/language |
CAPTCHA_THEME | light |
The Captcha checkbox theme. Supported: light , dark |
CAPTCHA_SIZE | normal |
The Captcha checkbox size. Supported: normal , compact |
Note: Laravel Captcha Does not support ReCaptcha V3
Usage
Displaying Captcha
Laravel Captcha Provide two blade directives for importing required javascript and displaying the captcha itself.
<head>
@captcha_js
head>
<body>
<form action="" method="post">
@captcha_container
form>
body>
@captcha_js
Directive
The The @captcha_js
directive allows you to inject the javascript for captcha. You can also pass additional language parameter into it.
@captcha_js('fr')
Read the HCaptcha & ReCaptcha docs to get the full language code list.
@captcha_container
Directive
The The @captcha_container
directive provides the captcha checkbox for your form. it also accepts two parameters theme
& size
respectively.
@captcha_container('light', 'compact')
Validation of Captcha
Laravel Captcha provides a very beautiful API to deal with captcha validation. In your Controller
or FormRequest
you can just add this code to validate the captcha.
use Rahul900day\Captcha\Facades\Captcha;
use Rahul900day\Captcha\Rules\Captcha as CaptchaRule;
use Illuminate\Support\Facades\Validator;
Validator::make($request, [
Captcha::getResponseName() => [
'required',
new CaptchaRule(),
],
]);
Testing
With Laravel Captcha you can write tests very easily. The Captcha
facade's fake
method allows you to fake the Captcha Validation for the current request.
use Rahul900day\Captcha\Facades\Captcha;
use Rahul900day\Captcha\Rules\Captcha as CaptchaRule;
use Illuminate\Support\Facades\Validator;
Captcha::fake();
// If $request is an actual request or a request array
$validation = Validator::make($request, [
Captcha::getResponseName() => [
'required',
new CaptchaRule(),
],
]);
$this->assertTrue(! $validation->fails()) // This is always going to pass.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.