Integrate Cloudflare Turnstile into Laravel and Livewire
Last updated on by Eric L. Barnes
Want to integrate Cloudflare Turnstile CAPTCHA into your Laravel or Livewire app? This package from Ryan Chandler makes integration super simple.
Cloudflare Turnstile for Laravel Usage
Include the Turnstile scripts using the @turnstileScripts
Blade directive in the <head>
of your layout file.
<html> <head> @turnstileScripts() </head> <body> {{ $slot }} </body></html>
After that, use the <x-turnstile />
component inside a <form>
:
<form action="/" method="POST"> <x-turnstile /> <button> Submit </button></form>
Use the provided validation rule on the server to validate the CAPTCHA response.
use Illuminate\Validation\Rule; public function submit(Request $request){ $request->validate([ 'cf-turnstile-response' => ['required', Rule::turnstile()], ]);}
Cloudflare Turnstile with Livewire
This package can also integrate seamlessly with Livewire. Upon successful validation, the property specified inside of wire:model
will be updated with the Turnstile token.
<x-turnstile wire:model="yourModel" />
Check out the package on GitHub for more details on customizing and integrating it into your app.
Eric is the creator of Laravel News and has been covering Laravel since 2012.