composer require mohammadv184/arcaptcha-laravel
php artisan vendor:publish --provider="Mohammadv184\Arcaptcha\ArcaptchaServiceProvider"
{!! ArCaptcha::render() !!}
$request->validate([
'arcaptcha' => 'required|captcha'
]);
Contact Form Protection
ArCaptcha::render() in your contact form Blade template.captcha rule.Blade Integration
ArCaptcha::render() in forms to generate the CAPTCHA widget.background, width, height).<div class="arcaptcha-container">
{!! ArCaptcha::render(['background' => '#f5f5f5']) !!}
</div>
Form Submission
captcha rule:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'arcaptcha' => 'required|captcha',
]);
Arcaptcha::verify($token):
if (!Arcaptcha::verify($request->input('arcaptcha'))) {
return response()->json(['error' => 'Invalid CAPTCHA'], 422);
}
Dynamic Rendering
$newToken = ArCaptcha::generate();
return view('form')->with(['arcaptcha_token' => $newToken]);
sometimes for conditional validation:
$request->validate([
'arcaptcha' => 'required_if:spam_protection,true|captcha',
]);
throttle middleware to prevent brute-force attacks.Arcaptcha::verify() in PHPUnit:
Arcaptcha::shouldReceive('verify')->once()->andReturn(true);
Token Mismatch
arcaptcha field) matches the server-side token.dd($request->input('arcaptcha'), ArCaptcha::getToken());
Configuration Overrides
config/arcaptcha.php) takes precedence over defaults.config(['arcaptcha.width' => 300]) for runtime overrides.Case Sensitivity
captcha) is case-sensitive. Use lowercase in rules.Caching Issues
session()->flush();
Enable Debug Mode:
Add to config/arcaptcha.php:
'debug' => env('ARCAPTCHA_DEBUG', false),
Logs errors to storage/logs/laravel.log.
Check Token Storage:
Verify tokens are stored in session('arcaptcha.token').
Custom Error Messages
Override validation messages in resources/lang/en/validation.php:
'captcha' => 'The CAPTCHA verification failed. Please try again.',
Extend ArCaptcha Class Bind a custom service provider to modify behavior:
// app/Providers/ArcaptchaServiceProvider.php
public function register()
{
$this->app->bind('arcaptcha', function () {
return new CustomArcaptcha();
});
}
Theme Customization
Extend the Arcaptcha class to support themes:
// app/Extensions/CustomArcaptcha.php
class CustomArcaptcha extends \Mohammadv184\Arcaptcha\Arcaptcha
{
public function render(array $options = [])
{
$options['theme'] = 'dark';
return parent::render($options);
}
}
Cache facade:
$cachedImage = Cache::remember('arcaptcha_image', 300, function () {
return ArCaptcha::render();
});
How can I help you explore Laravel packages today?