Installation
composer require mydaniel/laravel-captcha
Publish the config file (optional but recommended for customization):
php artisan vendor:publish --provider="MyDaniel\Captcha\CaptchaServiceProvider"
First Use Case: Basic Text Captcha Add the captcha to a form in your Blade template:
{!! captcha() !!}
Validate the submission in your controller:
use MyDaniel\Captcha\Rules\Captcha as CaptchaRule;
$request->validate([
'captcha' => [new CaptchaRule],
]);
Where to Look First
config/captcha.php (for customization).Captcha::generate() (for dynamic generation).MyDaniel\Captcha\Rules\Captcha (for form validation).Form Protection
{!! captcha(['type' => 'math', 'length' => 5]) !!}
$request->validate(['captcha' => new CaptchaRule]);
Dynamic Generation Use the facade to generate captchas programmatically:
$captcha = Captcha::generate('text', ['length' => 6]);
return response()->json(['captcha' => $captcha]);
API Endpoints Secure API endpoints with captchas:
public function store(Request $request) {
$request->validate([
'captcha' => new CaptchaRule,
'data' => 'required'
]);
// Process data...
}
Custom Generators Extend the package by creating a new generator:
use MyDaniel\Captcha\Contracts\Generator;
class CustomGenerator implements Generator {
public function generate(): string { /* ... */ }
}
Register it in config/captcha.php:
'generators' => [
'custom' => \App\Generators\CustomGenerator::class,
],
Multi-Language Validation Override validation messages in your language files:
'validation' => [
'captcha' => 'The captcha does not match.',
],
Captcha Not Showing?
captcha facade is registered in config/app.php under aliases.CaptchaServiceProvider is registered in config/app.php under providers.Validation Failing Silently
captcha field name matches the one generated in the form (default: captcha).'text' vs 'math').Replay Attacks
expiration_time in config/captcha.php for testing:
'expiration_time' => 60 * 5, // 5 minutes
Image Distortion Issues
distortion_level in the config:
'generators' => [
'text' => [
'distortion_level' => 0.3, // Lower = less distortion
],
],
Custom Validation Logic
Extend the CaptchaRule class to add custom logic:
use MyDaniel\Captcha\Rules\Captcha as BaseCaptchaRule;
class CustomCaptchaRule extends BaseCaptchaRule {
public function passes($attribute, $value) {
// Custom logic...
return parent::passes($attribute, $value);
}
}
Storage Backend Override the default storage (e.g., switch from session to Redis):
'storage' => [
'driver' => 'redis',
'connection' => 'cache',
],
Event Listeners Listen for captcha generation/validation events:
use MyDaniel\Captcha\Events\CaptchaGenerated;
CaptchaGenerated::listen(function ($event) {
Log::info('Captcha generated: ' . $event->captcha);
});
$generator = app(MyDaniel\Captcha\Generators\TextGenerator::class);
distortion_level for better performance (but less security).'text', 'math') are case-sensitive.length = 6, expiration_time = 600 seconds).SESSION_DRIVER is set in .env (e.g., SESSION_DRIVER=file for testing).How can I help you explore Laravel packages today?