buyownex/recaptcha

A Laravel 5.5 package providing support for reCAPTCHA.

1.1 2020-04-25 23:17 UTC

This package is auto-updated.

Last update: 2024-09-26 09:14:19 UTC


README

A Laravel 5.5 package for working with reCAPTCHA. Enjoy!

demo

Installation

composer require BuyOwnEx/recaptcha
./artisan vendor:publish --provider "Ilya\Recaptcha\RecaptchaServiceProvider"

Usage

First, edit the published config file config/recaptcha.php (tip: use env variables to hide keys from source control).

Then, in your controller:

use Ilya\Recaptcha\RecaptchaRule as Recaptcha;

// in a method...
$data = request()->validate([
    recaptcha_input() => ['required', new Recaptcha],
    // or...
    recaptcha_input() => ['required', new Recaptcha('key_pair_name')],

    // your other stuff here...
]);

On the client side, use recaptcha_script() to load the necessary JS code, and use recaptcha() or recaptcha('key_pair_name') to render the reCAPTCHA itself. Example:

<body>
    <form method="POST" action="...">
        {{ csrf_field() }}
        {{ recaptcha() }}

        <button>Send</button>
    </form>

    {{ recaptcha_script() }}
</body>

Also, to make error messages more friendly, add the following code to the custom section of your validation.php language file (resources/lang/en/validation.php if assuming that you're using the en locale):

'custom' => [
    // other stuff...

    recaptcha_input() => [
        'required' => 'Failed the reCAPTCHA test.',
    ],
],

Additional Information

MIT license.