1. Introduction
Sometimes we need to use verification code in the form submission to prevent flooding, bots and other malicious operations, about the verification code has a lot of open source Library to use, the current most used is the Google recaptcha--whether the client or server-side use is very simple and convenient, So here we take Google reCAPTCHA as an example of how to embed a verification code in a form that is laravel applied.
There are ready-to-integrate Google reCAPTCHA to Laravel projects on GitHub: Anhskohbo/no-captcha. In this tutorial we will demonstrate how to use the Captcha in Laravel 5.
2. Installation & Configuration
We use composer to install the expansion pack:
Composer require Anhskohbo/no-captcha 2.*
After the installation is complete, we need to register the service provider to the providers array in config/app.php:
Anhskohbo\nocaptcha\nocaptchaserviceprovider::class
Then need to get Google reCAPTCHA site key and Secret-key for the website, login reCAPTCHA Admin, first get need to register the site:
Click "Register" to get the site key and secret key for the website:
Add the site key and secret key you just acquired to the. env file:
Nocaptcha_secret=[secret-key]nocaptcha_sitekey=[site-key]
So we've configured the reCAPTCHA for the Laravel app, and we'll show the verification code in the form below.
3. Integrated verification code in the form
To display the verification code in the view, you need to 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::p ost (' Contact ', ' enquirycontroller@index ');
Then we define a controller Enquirycontroller:
' Required ', ' email ' = ' required|email ', ' subject ' = ' required ', ' g-recaptcha-response ' and ' required| ' Captcha ', ' msg ' = ' required ',); $validator = Validator::make ($data, $rules); if ($validator->fails ()) { Return redirect::to ('/contact ')->withinput ()->witherrors ($validator);} else{ //Do Your stuff.}}}
Finally we create a view file resources/views/contact.blade.php, and edit its contents 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 (' 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
Accessing the http://laravelacademy.org/contact in the browser shows the following effects: