Before integrating Laravel with Geetest CAPTCHA, we need to complete some preparation tasks.
First, create a Laravel project in your local environment by running the following command:
composer create-project --prefer-dist laravel/laravel geetest-demo
This will create a Laravel project named geetest-demo.
Next, install the Geetest CAPTCHA Laravel plugin by running the following command in the project root directory:
composer require geetest-laravel/geetest-laravel
This will add the necessary dependencies for Geetest to the project.
Once the preparation work is complete, we can begin configuring Geetest CAPTCHA.
In the config/app.php file, find the providers array and add the following line of code:
GeetestLaravel\GeetestLaravelServiceProvider::class,
This will register the Geetest CAPTCHA service provider.
Run the following command to generate the configuration file:
php artisan vendor:publish --provider="GeetestLaravel\GeetestLaravelServiceProvider"
After running the command, a geetest.php file will be generated in the config directory.
Open the geetest.php file and locate the id and key fields, and set them to the ID and KEY you obtain from Geetest's official website:
'id' => 'Your ID',
'key' => 'Your KEY',
Replace Your ID and Your KEY with the actual ID and KEY you received from Geetest.
Now, let’s use Geetest CAPTCHA in the Laravel views.
To display the CAPTCHA in a form, use the following code:
@if(geetest_captcha())
{!! geetest_init() !!}
@endif
This code will display the CAPTCHA input box and the verify button.
When the form is submitted, we need to validate the user’s CAPTCHA input. You can do this using the following code:
$validatedData = $request->validate([
'geetest_challenge' => 'required|geetest',
]);
This code will verify the user’s CAPTCHA. If the validation is successful, the next steps will continue.
With that, you’ve successfully integrated Geetest CAPTCHA into your Laravel project. In this article, we walked through how to create a project, install the plugin, configure Geetest, and use CAPTCHA in views for user validation. We hope this guide was helpful!