Install the Package Run:
composer require 2latlantik/recaptcha
Register the Bundle
Add to config/bundles.php:
Delatlantik\RecaptchaBundle\RecaptchaBundle::class => ['all' => true],
Configure Keys
Add your Google reCAPTCHA keys to config/packages/recaptcha.yaml:
recaptcha:
key: "your_public_key_here"
secret: "your_private_key_here"
First Use Case Integrate into a Symfony form:
use Delatlantik\RecaptchaBundle\Form\Type\RecaptchaSubmitType;
$builder->add('recaptcha', RecaptchaSubmitType::class);
Form Integration
RecaptchaSubmitType in Symfony forms for seamless validation.$form = $this->createFormBuilder()
->add('name', TextType::class)
->add('recaptcha', RecaptchaSubmitType::class, [
'label' => 'Verify you are not a robot',
])
->getForm();
Invisible reCAPTCHA
Custom Validation
RecaptchaSubmitType or using Symfony’s validation constraints:
use Delatlantik\RecaptchaBundle\Validator\Constraints\Recaptcha;
$builder->add('recaptcha', RecaptchaSubmitType::class, [
'constraints' => [
new Recaptcha(),
],
]);
API Integration
$this->get('recaptcha.recaptcha')->verify($token);
recaptcha_widget function in templates:
{{ form_widget(form.recaptcha) }}
recaptcha.yaml in config/{env}/).RecaptchaSubmitType:
->add('recaptcha', RecaptchaSubmitType::class, [
'error_bubbling' => true,
'error_mapping' => [
'recaptcha' => 'recaptcha.error',
],
])
Key Mismatch
public_key and private_key match your Google reCAPTCHA settings. Test with the reCAPTCHA demo.config/packages/recaptcha.yaml for typos.Version Conflicts
symfony/webpack-encore for v3).CSRF Issues
Deprecated PHP
APP_DEBUG=true) to log reCAPTCHA API responses.$response = $this->get('recaptcha.recaptcha')->verify($token);
dd($response->isSuccess()); // Should return true for valid tokens
Custom Services
RecaptchaInterface implementation:
$this->app->bind(\Delatlantik\RecaptchaBundle\Service\RecaptchaInterface::class, CustomRecaptchaService::class);
Event Listeners
$events->dispatch(new RecaptchaEvent($response));
Configuration Overrides
recaptcha.yaml with custom options (e.g., score_threshold for v3):
recaptcha:
key: "public_key"
secret: "private_key"
options: { "score_threshold": 0.5 } # Example for v3
recaptcha:verify command or mock the service in tests:
$this->mock(\Delatlantik\RecaptchaBundle\Service\RecaptchaInterface::class)
->shouldReceive('verify')
->andReturn(new Response(true, 'Success'));
6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI) to avoid hitting API limits.How can I help you explore Laravel packages today?