noylecorp/laravel-recaptcha

Laravel package for Google's ReCAPTCHA service

v0.4.1 2016-06-15 14:44 UTC

This package is not auto-updated.

Last update: 2024-11-09 18:06:34 UTC


README

Build Status

Laravel Recaptcha

Laravel 5.1 package for Google reCAPTCHA, providing helper functions for creating reCAPTCHA fields and a service for validating responses.

Note: This package uses reCAPTCHA API version 2.0

Installation

Install Laravel Recaptcha with composer:

composer require noylecorp/laravel-recaptcha

Next, add the service provider to your config/app.php:

'providers' => [
    // ...
    Noylecorp\Recaptcha\RecaptchaServiceProvider::class,
]

Next, publish the package configuration file to your application:

php artisan vendor:publish --provider="Noylecorp\Recaptcha\RecaptchaServiceProvider"

The file recaptcha.php gets copied into your configuration directory. The final installation step is to add your reCAPTCHA site and secret keys to your .env file:

RECAPTCHA_SITE_KEY=your-site-key
RECAPTCHA_SECRET_KEY=your-secret-key

If you don't have reCAPTCHA keys you can signs up for a pair here.

Usage

Creating a reCAPTCHA widget

Easily create reCAPTCHA widgets using the recaptcha() helper function:

{!! recaptcha() !!}

// outputs...

<script src="https://www.google.com/recaptcha/api.js"></script>
<div class="g-recaptcha" data-sitekey="my-site-key"></div>

You can also pass in HTML attributes...

{!! recaptcha(['id' => 'myrecaptcha']) !!}

...or any of reCAPTCHA's available options:

{!! recaptcha(['theme' => 'dark']) !!}

{!! recaptcha(['data-theme' => 'dark']) !!} // same thing

If you need to render the <script> and <div> tags for the reCAPTCHA widget separately, you can use the recaptch_script() and recaptcha_widget() functions:

{!! recaptcha_script() !!}

// ...

{!! recaptcha_widget() !!}

Need to support users without JavaScript? You can also insert a <noscript> fallback:

{!! recaptcha() !!}
{!! recaptcha_noscript() !!}

Or, if you don't want to use any of those helper functions, you can use recaptcha_site_key() to grab the site key for your own custom markup:

<div class="g-recaptcha" data-sitekey="{{ recaptcha_site_key() }}"></div>

Finally, if you have LaravelCollective Html installed, all helper functions can also be accessed through the Form facade:

{!! Form::recaptcha() !!}

Validating reCAPTCHA responses

The simplest way to validate a reCAPTCHA field is by using the added recaptcha validation rule:

// in a controller...
$this->validate($request, [
    'g-recaptcha-response' => 'required|recaptcha'
]);

// in a form request...
public function rules()
{
    return [
        'g-recaptcha-response' => 'required|recaptcha'
    ];
}

Or, you can access the service directly to do manual validation:

if (app('recaptcha')->verify($request->input('g-recaptcha-response'))) {
    // user passed reCAPTCHA
}
else {
    // user failed reCAPTCHA
}