cors/friendly-captcha-bundle
Installation:
composer require cors/friendly-captcha-bundle
Register the bundle in config/bundles.php (Symfony 4.3+):
return [
// ...
CORS\Bundle\FriendlyCaptchaBundle\CORSFriendlyCaptchaBundle::class => ['all' => true],
];
Configuration (.env or config/packages/cors_friendly_captcha.yaml):
cors_friendly_captcha:
sitekey: "YOUR_SITEKEY"
secret: "YOUR_SECRET"
use_eu_endpoints: true
First Use Case:
Add the Twig field to a form (e.g., resources/views/contact.html.twig):
{{ render(controller('CORS\Bundle\FriendlyCaptchaBundle\Controller\FriendlyCaptchaController::renderCaptcha')) }}
Validate the response in a controller:
use CORS\Bundle\FriendlyCaptchaBundle\Validator\Constraints\FriendlyCaptcha;
#[Route('/contact', name: 'contact')]
public function contact(Request $request): Response
{
$form = $this->createFormBuilder()
->add('name', TextType::class)
->add('email', EmailType::class)
->add('captcha', FriendlyCaptchaType::class) // Auto-injected by bundle
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Handle submission
}
// ...
}
Form Integration:
FriendlyCaptchaType in Symfony forms (auto-validates via FriendlyCaptcha constraint).$builder->add('captcha', FriendlyCaptchaType::class, [
'mapped' => false, // Captcha is not a form field (hidden)
'label' => 'Verify you are human',
]);
Dynamic Rendering:
{{ render(controller('CORS\Bundle\FriendlyCaptchaBundle\Controller\FriendlyCaptchaController::renderCaptcha', {
'theme': 'dark', // Optional: 'light'|'dark'
'size': 'normal' // Optional: 'normal'|'compact'
})) }}
API Validation:
use Symfony\Component\Validator\Constraints as Assert;
#[Assert\FriendlyCaptcha(
message: 'Captcha validation failed.',
siteSecret: '%env(FRIENDLY_CAPTCHA_SECRET)%'
)]
public $captchaResponse;
Custom Themes/Endpoints:
cors_friendly_captcha:
puzzle:
eu_endpoint: "https://your-custom-endpoint.com/puzzle"
Laravel-Specific Adaptation (Symfony bundle in Laravel):
spatie/symfony-bundle to integrate Symfony bundles in Laravel.config/app.php:
'providers' => [
// ...
CORS\Bundle\FriendlyCaptchaBundle\CORSFriendlyCaptchaBundle::class,
],
php artisan vendor:publish --provider="CORS\Bundle\FriendlyCaptchaBundle\CORSFriendlyCaptchaBundle"
Missing .env Configuration:
sitekey and secret are set in .env or config. The bundle throws InvalidArgumentException if missing..env:
FRIENDLY_CAPTCHA_SITEKEY=your_sitekey
FRIENDLY_CAPTCHA_SECRET=your_secret
CORS Issues:
eu-api.friendlycaptcha.eu.curl to test:
curl -X POST https://eu-api.friendlycaptcha.eu/api/v1/siteverify -d "secret=YOUR_SECRET&response=TOKEN"
Validation Failures:
FriendlyCaptcha constraint fails silently if the response is invalid. Add custom error messages:
#[Assert\FriendlyCaptcha(
message: 'The captcha response is invalid. Please try again.',
siteSecret: '%env(FRIENDLY_CAPTCHA_SECRET)%'
)]
Laravel-Specific:
spatie/symfony-bundle or manually bind services in AppServiceProvider:
public function register()
{
$this->app->register(CORSFriendlyCaptchaBundle::class);
}
Log Responses:
config/packages/cors_friendly_captcha.yaml:
cors_friendly_captcha:
debug: true
storage/logs/laravel.log for API response details.Manual API Testing:
curl -X POST https://api.friendlycaptcha.com/api/v1/siteverify \
-d "secret=YOUR_SECRET" \
-d "response=TOKEN_FROM_FRONTEND"
{"success":true,"error-codes":[],"challenge_ts":"2025-06-17T12:00:00Z"}
Theme/Size Issues:
theme and size parameters are supported (see FriendlyCaptcha docs).Custom Validation Logic:
FriendlyCaptchaValidator service:
// config/services.yaml
CORS\Bundle\FriendlyCaptchaBundle\Validator\FriendlyCaptchaValidator:
arguments:
$customLogic: '@app.custom_captcha_validator'
class CustomCaptchaValidator
{
public function validate(string $response, string $secret): bool
{
// Add custom logic (e.g., IP whitelisting)
return true;
}
}
Override Templates:
php artisan vendor:publish --tag=cors_friendly_captcha.templates
templates/captcha.html.twig to customize rendering.Event Listeners:
captcha.validated):
use CORS\Bundle\FriendlyCaptchaBundle\Event\CaptchaEvent;
public function onCaptchaValidated(CaptchaEvent $event)
{
if (!$event->isValid()) {
// Log failed attempts
}
}
config/services.yaml:
listeners:
CORS\Bundle\FriendlyCaptchaBundle\Event\CaptchaEvent::CAPTCHA_VALIDATED:
- [App\Listener\CaptchaListener, onCaptchaValidated]
How can I help you explore Laravel packages today?