anhskohbo/no-captcha
Laravel package to integrate Google reCAPTCHA (No CAPTCHA) in your forms. Includes config via .env, Blade helpers to render JS and display widgets, supports language and custom attributes, and works with Laravel 5+ (auto-discovery on 5.5+).
Installation:
composer require anhskohbo/no-captcha
(Auto-discovery enabled for Laravel 5.5+; otherwise, register the service provider and facade in config/app.php.)
Configuration:
Add to .env:
NOCAPTCHA_SECRET=your_secret_key
NOCAPTCHA_SITEKEY=your_site_key
(Obtain keys from Google reCAPTCHA Admin.)
Publish Config (if not using auto-discovery):
php artisan vendor:publish --provider="Anhskohbo\NoCaptcha\NoCaptchaServiceProvider"
First Use Case: Add reCAPTCHA to a form in Blade:
{!! NoCaptcha::renderJs() !!}
{!! NoCaptcha::display() !!}
Validate the response in a controller:
$validator = Validator::make(request()->all(), [
'g-recaptcha-response' => 'required|captcha',
]);
<!-- Load JS (once per page) -->
{!! NoCaptcha::renderJs('en', false, 'onloadCallback') !!}
<!-- Display widget -->
{!! NoCaptcha::display(['data-theme' => 'light', 'data-size' => 'compact']) !!}
$rules = ['g-recaptcha-response' => 'required|captcha'];
$validator = Validator::make($request->all(), $rules);
Blade Integration:
{!! NoCaptcha::displaySubmit('contact-form', 'Submit', ['data-theme' => 'dark']) !!}
(Requires id="contact-form" on the form.)
Validation: Same as standard flow; the package auto-triggers validation on form submission.
renderJs():
{!! NoCaptcha::renderJs('fr') !!}
resources/lang/en/validation.php:
'custom' => [
'g-recaptcha-response' => [
'required' => 'Veuillez prouver que vous n\'êtes pas un robot.',
'captcha' => 'Erreur de vérification. Réessayez ou contactez l\'admin.',
],
],
NoCaptcha::shouldReceive('verifyResponse')->andReturn(true);
$response = $this->post('/register', [
'g-recaptcha-response' => '1', // Mock response
'email' => 'test@example.com',
]);
g-recaptcha-response in the controller.displaySubmit().displaySubmit() argument.NoCaptcha class or manually register the service provider.data-sitekey attribute manually:
<div class="g-recaptcha" data-sitekey="{{ config('services.nocaptcha.sitekey') }}"></div>
config('services.nocaptcha.sitekey') per environment or feature flag.Missing JS Rendering:
NoCaptcha::renderJs() is called before NoCaptcha::display() in the Blade template.grecaptcha errors (e.g., missing API key).Form ID Mismatch (Invisible reCAPTCHA):
id matches the first argument in displaySubmit('form-id', ...).data-formid attributes.Validation Field Name Hardcoding:
g-recaptcha-response; custom field names require middleware:
$request->merge(['g-recaptcha-response' => $request->input('custom_captcha_field')]);
Testing Pitfalls:
g-recaptcha-response in HTTP requests.$this->post('/register', ['g-recaptcha-response' => '1', ...]);
verifyResponse and display:
NoCaptcha::shouldReceive('verifyResponse')->andReturn(true);
NoCaptcha::shouldReceive('display')->andReturn('<input type="hidden" name="g-recaptcha-response" value="1" />');
Secret Key Exposure:
.env keys may be committed accidentally..env to .gitignore and use php artisan config:clear after key changes.Invisible reCAPTCHA Edge Cases:
onsubmit or data-callback attributes align with the package’s expectations.reCAPTCHA Errors:
.env:
NOCAPTCHA_DEBUG=true
$response = NoCaptcha::verifyResponse($request->input('g-recaptcha-response'));
\Log::debug('reCAPTCHA Response', ['success' => $response, 'input' => $request->input('g-recaptcha-response')]);
Validation Errors:
captcha rule is correctly applied:
$validator = Validator::make($request->all(), [
'g-recaptcha-response' => 'required|captcha',
]);
Auto-Discovery:
composer.json.php artisan vendor:publish --tag="nocaptcha-config"
Language Fallback:
en. Override in renderJs():
{!! NoCaptcha::renderJs('fr') !!}
Invisible reCAPTCHA:
Custom Validation Logic:
captcha rule by creating a custom validator:
use Anhskohbo\NoCaptcha\Rules\Recaptcha;
$rules = ['g-recaptcha-response' => [new Recaptcha]];
Middleware for Dynamic Keys:
app('nocaptcha')->setSiteKey('dynamic-key-' . $user->id);
Event Listeners:
NoCaptcha::failed(function ($response) {
\Log::warning('reCAPTCHA failed', ['response' => $response]);
});
Alternative CAPTCHA Providers:
verifyResponse method by extending the NoCaptcha class:
class CustomNoCaptcha extends \Anhskohbo\NoCaptcha\NoCaptcha {
public
How can I help you explore Laravel packages today?