Install the package via Composer:
composer require vendor/package-name
Publish the configuration file (if available) and run migrations:
php artisan vendor:package publish --provider="Vendor\PackageName\PackageServiceProvider"
php artisan migrate
For Google reCAPTCHA, register your site keys in .env:
RECAPTCHA_SITE_KEY=your_site_key
RECAPTCHA_SECRET_KEY=your_secret_key
Use the reCAPTCHA validation in a form request:
use Vendor\PackageName\Http\Requests\RecaptchaValidatedFormRequest;
class MyFormRequest extends RecaptchaValidatedFormRequest
{
public function rules()
{
return [
'email' => 'required|email',
// Other rules...
];
}
}
Extend RecaptchaValidatedFormRequest to auto-validate reCAPTCHA submissions:
// In your controller
public function store(MyFormRequest $request)
{
// reCAPTCHA is validated automatically
// Proceed with logic...
}
Validate reCAPTCHA manually in controllers:
use Vendor\PackageName\Services\RecaptchaService;
public function submit(Request $request, RecaptchaService $recaptcha)
{
if (!$recaptcha->verify($request->input('g-recaptcha-response'))) {
return back()->withErrors(['recaptcha' => 'Invalid verification']);
}
// Proceed...
}
Override the default site key per request:
$recaptcha->verify($response, 'custom_site_key');
Embed reCAPTCHA in Blade templates:
<div class="g-recaptcha" data-sitekey="{{ config('services.recaptcha.site_key') }}"></div>
.env keys are set before first use (e.g., in bootstrap/app.php or AppServiceProvider).config/services.php (add recaptcha array if missing).g-recaptcha-response field name in form submissions.secret_key in .env.https://www.google.com/recaptcha/api/siteverify (use browser dev tools).RecaptchaService to modify error messages or logic:
$recaptcha = app()->makeWith(RecaptchaService::class, [
'customError' => 'Custom error message',
]);
secret_key).RecaptchaService::fake() in PHPUnit:
public function testRecaptchaValidation()
{
RecaptchaService::fake()->shouldVerifySuccessfully();
// Test logic...
}
score thresholds:
$recaptcha->verify($token, 0.9); // Minimum score
How can I help you explore Laravel packages today?