ady/captcha-bundle
Laravel bundle providing CAPTCHA integration to protect forms from bots. Includes configurable drivers, easy validation rules, and middleware-friendly setup. Helps reduce spam on logins, registrations, contact forms, and other public endpoints.
Installation
composer require ady/captcha-bundle
Add to config/bundles.php (Symfony):
return [
// ...
Ady\CaptchaBundle\AdyCaptchaBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console ady:captcha:install
Edit config/packages/ady_captcha.yaml to adjust:
provider (e.g., hcaptcha, recaptcha_v3)hcaptcha.site_key, hcaptcha.secret_key)First Use Case Add the Twig extension to your template:
{{ ady_captcha('hcaptcha', {
'theme': 'light',
'size': 'compact'
}) }}
Validate in a controller:
use Ady\CaptchaBundle\Validator\Constraints as CaptchaAssert;
/**
* @CaptchaAssert\ValidCaptcha(
* provider="hcaptcha",
* message="Invalid CAPTCHA."
* )
*/
public function submitForm(Request $request)
{
// ...
}
Form Integration
CaptchaAssert\ValidCaptcha constraint on form handlers or DTOs.$builder->add('captcha', CaptchaType::class, [
'provider' => 'recaptcha_v3',
'score_threshold' => 0.9,
]);
Dynamic Provider Selection
# config/packages/ady_captcha.yaml
ady_captcha:
provider: '%env(ADY_CAPTCHA_PROVIDER)%'
$validator->enableConstraint(new ValidCaptcha('hcaptcha'));
API/Non-Twig Usage
$token = $this->container->get('ady_captcha.token_generator')->generate('hcaptcha');
$validator = $this->container->get('ady_captcha.validator');
$isValid = $validator->validate($token, $request->request->get('captcha_response'));
Custom Providers
Ady\CaptchaBundle\Provider\AbstractProvider:
class MyProvider extends AbstractProvider
{
public function getName(): string { return 'my_provider'; }
public function verify(string $token, string $response): bool { /* ... */ }
}
services.yaml:
services:
Ady\CaptchaBundle\Provider\MyProvider:
tags: ['ady_captcha.provider']
Rate Limiting
Ady\CaptchaBundle\EventListener\RateLimitListener to throttle requests:
ady_captcha:
rate_limiter:
enabled: true
max_attempts: 5
window_size: 3600
Token Expiry
ady_captcha:
token_ttl: 300 # 5 minutes
Provider-Specific Quirks
data-sitekey attribute in HTML (not data-captcha).0.0 and 1.0.verify() returns bool and handles exceptions.CSRF Token Conflicts
CSRFTokenManager, ensure CAPTCHA tokens don’t collide:
ady_captcha:
token_name: 'captcha_token' # Default is '_captcha'
Debugging Failures
ady_captcha:
debug: true
$validator->validate($token, $response, true); // Returns raw provider response
Caching Issues
php bin/console cache:clear
Environment-Specific Keys
Use Symfony’s %env% for keys:
ady_captcha:
hcaptcha:
site_key: '%env(HCAPTCHA_SITE_KEY)%'
secret_key: '%env(HCAPTCHA_SECRET_KEY)%'
Testing
$validator = $this->createMock(ValidatorInterface::class);
$validator->method('validate')->willReturn(true);
$this->container->set('ady_captcha.validator', $validator);
TestProvider for unit tests:
ady_captcha:
provider: 'test'
Performance
services:
Ady\CaptchaBundle\Provider\HcaptchaProvider:
lazy: true
Accessibility
{{ ady_captcha('hcaptcha', {
'aria-label': 'Prove you are not a robot'
}) }}
Fallback Mechanisms
if (!$validator->validate($token, $response) && $this->isTrustedIP($request->getClientIp())) {
// Bypass CAPTCHA for trusted users
}
How can I help you explore Laravel packages today?