Installation
composer require mohammadv184/arcaptcha
Add the service provider to config/app.php:
'providers' => [
// ...
Mohammadv184\Arcaptcha\ArcaptchaServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Mohammadv184\Arcaptcha\ArcaptchaServiceProvider" --tag="arcaptcha-config"
Update config/arcaptcha.php with your ArCaptcha API keys and site settings.
First Use Case: Basic Verification
use Mohammadv184\Arcaptcha\Facades\Arcaptcha;
$token = request('arcaptcha-token');
$result = Arcaptcha::verify($token);
if ($result) {
// Valid CAPTCHA, proceed with form submission
} else {
// Invalid CAPTCHA, show error
}
Blade Integration Add the CAPTCHA widget to your form:
{!! Arcaptcha::render() !!}
Form Submission Validation Integrate with Laravel's validation pipeline:
$request->validate([
'arcaptcha-token' => 'required|arcaptcha',
]);
(Ensure the ArcaptchaRule is registered in AppServiceProvider.)
Dynamic CAPTCHA Rendering Customize CAPTCHA appearance via config:
Arcaptcha::render([
'theme' => 'dark',
'size' => 'compact',
]);
API Endpoint Protection Protect API routes with CAPTCHA:
Route::post('/api/submit', function () {
$token = request('arcaptcha-token');
if (!Arcaptcha::verify($token)) {
return response()->json(['error' => 'Invalid CAPTCHA'], 403);
}
// Proceed with API logic
});
Rate Limiting Integration Combine with Laravel's rate limiting:
Route::middleware(['throttle:60,1', 'arcaptcha'])->group(function () {
// Rate-limited and CAPTCHA-protected routes
});
Custom Error Handling Override default error messages:
Arcaptcha::setErrorMessage('custom.error.message');
Arcaptcha::fake() in tests to simulate valid/invalid responses:
Arcaptcha::fake(['valid' => true]); // or false
API Key Exposure
config/arcaptcha.php to version control. Use environment variables:
ARCAPTCHA_SITE_KEY=your_site_key
ARCAPTCHA_SECRET_KEY=your_secret_key
Token Expiry
CSRF Conflicts
csrf_exclude middleware.Deprecated Methods
Network Errors
try {
$result = Arcaptcha::verify($token);
} catch (\Exception $e) {
// Log error, show fallback CAPTCHA or manual verification
}
Enable Debug Mode
Set debug to true in config/arcaptcha.php to log API responses.
Manual Verification Test tokens via ArCaptcha's demo page to isolate issues.
HTTP Client Inspection Override the HTTP client for debugging:
Arcaptcha::setClient(new \GuzzleHttp\Client(['debug' => true]));
Custom Themes
Extend the ArcaptchaServiceProvider to add theme support:
public function boot()
{
Arcaptcha::extend('custom-theme', function () {
return '<div class="custom-captcha">...</div>';
});
}
Event Listeners Listen for CAPTCHA events (if the package emits them):
Arcaptcha::verified(function ($token) {
// Log successful verifications
});
Fallback Mechanisms Implement a fallback (e.g., simple text CAPTCHA) if ArCaptcha fails:
if (!Arcaptcha::verify($token)) {
return view('fallback-captcha');
}
Multi-Site Support Use config groups to manage multiple ArCaptcha sites:
Arcaptcha::setSite('secondary-site')->verify($token);
How can I help you explore Laravel packages today?