Laravel – Google reCaptcha Code using anhskohbo/no-captcha
In this post we will give you information about Laravel – Google reCaptcha Code using anhskohbo/no-captcha. Hear we will give you detail about Laravel – Google reCaptcha Code using anhskohbo/no-captchaAnd how to use it also give you demo for it if it is necessary.
I think we should use Google captcha code on our registration form, contact form etc because captcha code prevent spams, bots etc. Most of the application we need to use captcha varification because it very important for security reason. There are several library to generate captcha image in Laravel. In this example i use Google reCaptcha for generate captcha using anhskohbo/no-captcha package. anhskohbo/no-captcha is very popular package.
In this post i give you very simple and from scratch of generate Google reCaptcha code as you can also see bellow preview. After complete this example you can found ui design like bellow preview, you have to just follow few step and find result:
Preview:
Step 1: Installation
In first step we will install anhskohbo/no-captcha package for Google reCaptcha code. this package through we can generate generate captcha code for our project. so first fire bellow command in your cmd or terminal:
composer require anhskohbo/no-captcha
Now we need to add provider path and alias path in config/app.php file so open that file and add bellow code.
config/app.php
return [
......
$provides => [
......
......,
AnhskohboNoCaptchaNoCaptchaServiceProvider::class
],
.....
]
Step 2: Set Google Site Key
In this step we need to set google site key and secret key. If you don’t have site key and secret key then you can create from here. First click on this link : Recaptcha Admin
After click you can see bello view and you need register your site link this way:
Ok, after sucessfully register you can get site key and secret key from like bellow preview.
Now open .env file and add this two variable
.env
NOCAPTCHA_SECRET=[secret-key]
NOCAPTCHA_SITEKEY=[site-key]
Step 3: Add Route
In second step we will add new two route for creating small example that way we can undestand very well. so first add bellow route in your routes.php file.
app/Http/routes.php
Route::get('site-register', 'AuthAuthController@siteRegister');
Route::post('site-register', 'AuthAuthController@siteRegisterPost');
Step 4: Add Controller Method
In this step we will add siteRegister() and siteRegisterPost() method in AuthController Controller file for our example. If you want to copy whole controller file then also you can just copy and paste.
app/Http/Controllers/Auth/AuthController.php
namespace AppHttpControllersAuth;
use Validator;
use AppHttpControllersController;
use IlluminateFoundationAuthThrottlesLogins;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;
use Auth;
use IlluminateHttpRequest;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return IlluminateContractsValidationValidator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
public function siteRegister()
{
return view('siteRegister');
}
public function siteRegisterPost(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|same:password_confirmation',
'password_confirmation' => 'required',
'g-recaptcha-response' => 'required|captcha',
]);
print('done');
}
}
Step 5: Add Blade file
This is a last step and you have to just create new blade file siteRegister.blade.php and put bellow code on that file.
resources/views/siteRegister.blade.php
@extends('layouts.app')
@section('content')
<div >
<div >
<div >
<div >
<div >Register</div>
<div >
<form role="form" method="POST" action="{{ url('/site-register') }}">
{!! csrf_field() !!}
<div >
<label >Name</label>
<div >
<input type="text" name="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<span >
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label >E-Mail Address</label>
<div >
<input type="email" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span >
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label >Password</label>
<div >
<input type="password" name="password">
@if ($errors->has('password'))
<span >
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label >Confirm Password</label>
<div >
<input type="password" name="password_confirmation">
@if ($errors->has('password_confirmation'))
<span >
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
<div >
<label >Captcha</label>
<div >
{!! app('captcha')->display() !!}
@if ($errors->has('g-recaptcha-response'))
<span >
<strong>{{ $errors->first('g-recaptcha-response') }}</strong>
</span>
@endif
</div>
</div>
<div >
<div >
<br/>
<button type="submit" >
<i ></i>Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Try this.. if you want to more google captcha then you can do from here : anhskohbo/no-captcha.
Hope this code and post will helped you for implement Laravel – Google reCaptcha Code using anhskohbo/no-captcha. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs