- How do I install and set up anhskohbo/no-captcha in Laravel 5.5+?
- Run `composer require anhskohbo/no-captcha`, then add your Google reCAPTCHA keys (`NOCAPTCHA_SECRET` and `NOCAPTCHA_SITEKEY`) to your `.env` file. Laravel 5.5+ auto-discovers the package, so no manual registration in `app.php` is needed.
- Can I use invisible reCAPTCHA with this package?
- Yes, use `NoCaptcha::displaySubmit()` in your Blade template to render an invisible reCAPTCHA widget. This is ideal for high-conversion forms like checkouts, as it triggers validation only on submission.
- How do I validate reCAPTCHA responses in Laravel forms?
- Add the `required|captcha` rule to your Form Request or validation array. The package integrates with Laravel’s validator, so no extra logic is needed in your controller.
- Does this package work with Laravel 4?
- No, this package is designed for Laravel 5.5+. For Laravel 4, use the `v1` branch of the package, which requires manual service provider and facade registration.
- How can I customize the reCAPTCHA widget appearance?
- Pass an array of attributes to `NoCaptcha::display()`, such as `['data-theme' => 'dark', 'data-size' => 'compact']`. This allows you to match the widget’s theme, size, and other Google-supported parameters to your app’s design.
- What if Google’s reCAPTCHA API changes or breaks?
- Monitor Google’s reCAPTCHA release notes for updates. If needed, fork the package or pin a specific version in `composer.json` (e.g., `^3.7`). The package’s modular design makes it easier to swap out reCAPTCHA for alternatives like hCaptcha.
- How do I test forms with reCAPTCHA in PHPUnit?
- Mock the `NoCaptcha` facade in your tests: `NoCaptcha::shouldReceive('verifyResponse')->andReturn(true)`. For validation tests, use hidden input fallbacks or disable CAPTCHA checks in test environments via `.env` overrides.
- What should I do if users report ad blockers breaking reCAPTCHA?
- Implement a server-side fallback, such as manual CAPTCHA or IP-based rate-limiting, for users who disable JavaScript. Test with ad blockers like uBlock Origin in your QA environment to catch these issues early.
- Can I enable/disable reCAPTCHA per form or route?
- Yes, use middleware to conditionally validate reCAPTCHA tokens. For example, create a `VerifyRecaptcha` middleware to wrap routes or forms where CAPTCHA is required, or disable it via configuration for specific forms.
- How do I handle reCAPTCHA validation for AJAX or API requests?
- Use middleware like `VerifyRecaptcha` to validate tokens on API routes. Ensure your AJAX requests include the `g-recaptcha-response` token in the payload, and validate it using `NoCaptcha::verifyResponse($token)` in your controller or middleware.