Hello Artisan,
Here, I will show you how to work Laravel 9 google ReCaptcha v3 with form validation step by step. In this Laravel 9 recaptcha v3 example I would like to show you Laravel 9 Google ReCaptcha example. From this Laravel 9 Google Recaptcha v3 tutorial, we will learn how to use Google Recaptcha v3 in Laravel 9.
You will see step by step ReCaptcha v3 in Laravel 9. Let's see below example of google recaptcha register laravel 9 application. If you don't know how to implement Google ReCaptcha V3 then this example is for you.
Google ReCaptcha V3 is a captcha-like system, that provides security against hackers and sticks or curls requests. In this example, we will create a simple registration form and implement a google captcha code.
In this tutorial, before using google captcha code we will install the josiasmontag/laravel-recaptchav3
composer package for google captcha. You need to just follow a few steps and you will get google re-captcha code in your laravel 9 app.
Step 1: Download Laravel 9
Download a fresh Laravel project by the following command:
composer create-project laravel/laravel example-app
Step 2: Install Package
In this step, we have to install josiasmontag/laravel-recaptchav3
via the composer, so open your terminal and run the bellow command:
composer require josiasmontag/laravel-recaptchav3
After successfully installing the package, we require to publish the config file with below command:
php artisan vendor:publish --provider="Lunaweb\RecaptchaV3\Providers\RecaptchaV3ServiceProvider"
Step 3: Create Google API Key
In this step, we need to create google api key before using this google recaptcha v3 service: link: Recaptcha Admin
Now you have to create a new key for google recaptchav3 as below screenshot:
Now create a new api key like:
Now you will get api key like below:
.env
RECAPTCHAV3_SITEKEY=[site-key]
RECAPTCHAV3_SECRET=[secret-key]
Step 4: Create Controller
In this step, we have to create a new controller as RegisterController and we have also need to add two methods index() and store() on that controller as you can see below:
app/Http/Controllers/RegisterController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function index()
{
return view('register');
}
/**
* Create a new controller instance.
*
* @return void
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'password' => 'required|same:password_confirmation',
'password_confirmation' => 'required',
'g-recaptcha-response' => 'required|recaptchav3:register,0.5'
]);
}
}
Step 5: Add Route
We need to create register routes as below:
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RegisterController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::controller(RegisterController::class)->group(function(){
Route::get('register', 'index');
Route::post('register', 'store');
});
Step 6: Create View File
In the last step, let's create register.blade.php for layout and lists all items code here and put the following code:
resources/views/register.blade.php
Now run php artisan serve
and test by visiting this url:
Read also: Laravel 9 Google ReCAPTCHA Tutorial Example
Hope this laravel recaptcha v3 example tutorial will help you.
#laravel #laravel-9x