He hecho una busqueda rapida y he encontrado, pensaba que sería algo mas simple(pero eso en el caso que usemos el de Google), pero por evitar tener que configurar alguno de nosotros la cuenta creo que es mejor hacer esto. Lo dejo aquí para que @christianpf cuando pueda lo implemente. Hay que estar atentos a cuando lo haga ya que como podeis ver se hace una instalación de una serie de paquetes por parte de composer(recuerdanos que tenemos que actualizar tras hacer el pull donde hayas hecho los cambios Christian.
Laravel 8 Captcha Tutorial – Add Captcha In Forms
Laravel 8 captcha tutorial. In this tutorial, we will show you how to add a captcha validation with laravel 8 forms.
The captcha provides security challenge on forms to prevent comment spamming, form spamming.
Laravel 8 Captcha – Add Captcha Validation in Form
Step 1 – Download Laravel 8 Application
Step 2 – Setup Database with App
Step 3 – Install Captcha Package
Step 4 – Register Captcha Package
Step 5 – Captcha Configuration
Step 6 – Create Form Routes
Step 7 – Create Form Controller By Artisan Command
Step 8 – Create Form Blade File
Step 9 – Run Development Server
Step 1 – Download Laravel 8 Application
First of all download or install laravel 8 new setup. So, open terminal and type the following command to install new laravel 8 app into your machine:
composer create-project --prefer-dist laravel/laravel FormValidation
Step 2 – Setup Database with App
In this step, setup database with your downloded/installed laravel 8 app. So, you need to find .env file and setup database details as following:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password
Step 3 – Install Captcha Package
In this step, open again your command prompt. And run the following command on it. To install captcha package:
composer require mews/captcha
Step 4 – Register Captcha Package
In this step, registered this package in laravel application. So, Open providers/config/app.php file and register the captcha service provider and aliases.
'providers' => [
...
...
...
Mews\Captcha\CaptchaServiceProvider::class,
]
'aliases' => [
...
...
...
'Captcha' => Mews\Captcha\Facades\Captcha::class,
Step 5 – Captcha Configuration
In this step, open config/captcha.php file. And in this file you can enable or disable settings based on your requirement:
return [
'default' => [
'length' => 5,
'width' => 120,
'height' => 36,
'quality' => 90,
'math' => true, //Enable Math Captcha
'expire' => 60, //Stateless/API captcha expiration
],
// ...
];
Step 6 – Create Routes
In this step, open web.php file from routes direcotry. And update the following routes into web.php file:
use App\Http\Controllers\CaptchaValidationController;
Route::get('contact-form-captcha', [CaptchaValidationController::class, 'index']);
Route::post('captcha-validation', [CaptchaValidationController::class, 'capthcaFormValidate']);
Route::get('reload-captcha', [CaptchaValidationController::class, 'reloadCaptcha']);
Step 7 – Create Form Controller By Artisan Command
In this step, run the following command on command prompt to create controller file:
php artisan make:controller CaptchaValidationController
After that, go to app/http/controllers and open CaptchaValidationController.php file. And update the following code into it:
validate([
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
'captcha' => 'required|captcha'
]);
}
public function reloadCaptcha()
{
return response()->json(['captcha'=> captcha_img()]);
}
}
Step 8 – Create Blade File
Now, create blade view file to display form with captcha challenge and submit to database. So, Go to resources/views and create form-with-captcha.blade.php and update the following code into it:
<title>Laravel 8 Form Captcha Validation</title>
@if(session('status'))
{{ session('status') }}
@endif
Laravel 8 Add Captcha in Form For Validation
@csrf
Name
@error('name')
{{ $message }}
@enderror
Email
@error('email')
{{ $message }}
@enderror
Message
<textarea name="message" class="
@error('message') is-invalid @enderror form-control"></textarea>
@error('message')
{{ $message }}
@enderror
Submit
<script type="text/javascript">
$('#reload').click(function () {
$.ajax({
type: 'GET',
url: 'reload-captcha',
success: function (data) {
$(".captcha span").html(data.captcha);
}
});
});
</script>
The following below code will display validation error message on blade view file:
@error('name')
{{ $message }}
@enderror
Step 9 – Run Development Server
Last step, open command prompt and run the following command to start developement server:
php artisan serve
Then open your browser and hit the following url on it:
http://127.0.0.1:8000/contact-form-captcha
Fuente: https://www.tutsmake.com/laravel-8-captcha-tutorial-add-captcha-in-forms/