Install the package:
composer require baks-dev/captcha
Publish assets and configuration (required for templates and storage):
php artisan baks:assets:install
public/baks/captcha/ directory and config file at config/baks/captcha.php.Basic usage in a form:
use BaksDev\Captcha\CaptchaBuilder;
// In your controller or service
$captcha = CaptchaBuilder::build();
$captcha->store(); // Stores in session by default
// In your Blade template
<img src="{{ $captcha->getImageUrl() }}" alt="CAPTCHA">
<input type="text" name="captcha_code" required>
Validate the submission:
use BaksDev\Captcha\CaptchaValidator;
$validator = CaptchaValidator::validate(request('captcha_code'));
if (!$validator->isValid()) {
return back()->withErrors(['captcha' => 'Invalid CAPTCHA']);
}
// Generate a new CAPTCHA per request (e.g., for AJAX forms)
$captcha = CaptchaBuilder::build([
'length' => 6, // Custom length
'font' => 'arial.ttf', // Custom font (must be in assets)
'width' => 120,
'height' => 40,
]);
$captcha->store('custom_key'); // Store with a custom key
// Using Laravel Collective or Livewire
{!! Form::captcha() !!} // If the package provides a helper (check docs)
rules().// Generate CAPTCHA for API responses (e.g., as a base64 image)
$captcha = CaptchaBuilder::build();
$image = $captcha->getImage();
return response($image)->header('Content-Type', 'image/png');
Override the default session storage:
$captcha = CaptchaBuilder::build();
$captcha->storeInDatabase($userId); // Hypothetical method (check docs)
Combine with Google ReCAPTCHA for extra security:
// Validate both CAPTCHAs
$validator = CaptchaValidator::validate(request('captcha_code'));
$recaptcha = app('recaptcha')->verify(request('g-recaptcha-response'));
AppServiceProvider if not auto-discovered:
if (!app()->has('baks.captcha')) {
$this->app->register(BaksDev\Captcha\CaptchaServiceProvider::class);
}
Blade::directive('captcha', function ($expression) {
$captcha = CaptchaBuilder::build();
return "<?php echo \$captcha->render(); ?>";
});
Usage:
@captcha
public function handle(Request $request, Closure $next) {
if ($request->is('login') && $request->isMethod('post')) {
$validator = CaptchaValidator::validate($request->captcha_code);
if (!$validator->isValid()) {
return back()->withErrors(['captcha' => 'Invalid']);
}
}
return $next($request);
}
php artisan baks:assets:install will break image generation.public/baks/captcha/ exists and is writable..ttf fonts in public/baks/captcha/fonts/.$validator = CaptchaValidator::validate(strtolower(request('captcha_code')));
throttle middleware).storage/logs/laravel.log for errors like missing GD library.sudo apt-get install php-gd # Ubuntu/Debian
sudo dnf install php-gd # CentOS/RHEL
\Log::info('Stored:', session('captcha_code'));
\Log::info('Submitted:', request('captcha_code'));
config/baks/captcha.php may conflict with defaults.php artisan config:clear after changes.2 + 3 = ?).class MathCaptcha extends CaptchaBuilder {
public function build() {
$this->setText($this->generateMathProblem());
// ...
}
}
// config/baks/captcha.php
'events' => [
'enabled' => true,
],
$this->partialMock(CaptchaValidator::class, 'validate')
->shouldReceive('isValid')
->andReturn(true);
$captcha = CaptchaBuilder::build(['locale' => 'es']);
php artisan vendor:publish --tag=baks-captcha-views
resources/views/vendor/baks/captcha.blade.php.How can I help you explore Laravel packages today?