How to integrate GooglereCAPTCHA verification code in Laravel5 forms 1. Introduction
Sometimes we need to use verification codes when submitting forms to prevent malicious operations such as irrigation and robots. There are many open-source libraries for verification codes, currently, Google reCAPTCHA, the most frequently used one, is easy to use on both the client and server. So here we use Google reCAPTCHA as an example to demonstrate how to embed a verification code in a Laravel application form.
Github has a ready-made project integrating Google reCAPTCHA to Laravel: anhskohbo/no-captcha. In this tutorial, we will demonstrate how to use the verification code in Laravel 5.
2. installation and configuration
We use Composer to install the extension package:
composer require anhskohbo/no-captcha 2.*
After the installation is complete, register the service provider in config/app. php to the providers array:
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class
Then you need to obtain the Google recaptcha site key and secret-key for the site, log on to Recaptcha Admin, and register the site for the first time:
Click "Register" to obtain the site key and secret key corresponding to the site:
Add the obtained site key and secret key to the. env file:
NOCAPTCHA_SECRET=[secret-key]NOCAPTCHA_SITEKEY=[site-key]
In this way, we have configured recaptcha for the Laravel application. the verification code is displayed in the form below.
3. integrate the verification code into the form
To display the verification code in the view, insert the following line of code:
{!! app('captcha')->display(); !!}
First, we define an access route in routes. php:
Route::get('contact', function() { return View::make('contact');});Route::post('contact', 'EnquiryController@index');
Then we define a controller EnquiryController:
'required', 'email' => 'required|email','subject' => 'required','g-recaptcha-response' => 'required|captcha','msg' => 'required',);$validator = Validator::make($data, $rules);if ($validator->fails()){ return Redirect::to('/contact')->withInput()->withErrors($validator);}else{ // Do your stuff.}}}
Finally, create a view file resources/views/contact. blade. php and edit the file as follows:
Contact us
@if (count($errors) > 0)
Whoops! There were some problems with your input.
@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach
@endif{!! Form::open(array('url'=>'contact','method'=>'POST', 'id'=>'myform')) !!}
Name
{!! Form::text('name','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}
E-Mail Address
{!! Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Email')) !!}
Subject
{!! Form::text('subject','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Subject')) !!}
Message
{!! Form::textarea('msg','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Your Full Name')) !!}
Captcha
{!! app('captcha')->display(); !!}
Submit
Access the http://laravelacademy.org/contact in a browser and the effect is as follows: