PHP code example of hansdeboeck / laravel-turnstile

1. Go to this page and download the library: Download hansdeboeck/laravel-turnstile library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hansdeboeck / laravel-turnstile example snippets


use HansDeBoeck\Turnstile\Facades\Turnstile;

// Als validatieregel, de kortste weg:
$request->validate([
    'turnstile_token' => 'oken'))) {
    return back()->withErrors(['turnstile_token' => 'Verificatie mislukt.']);
}

// Met het volledige resultaat:
$result = Turnstile::verify($token);
$result->passed;      // bool
$result->skipped;     // geslaagd omdat de omgeving de controle overslaat
$result->failure;     // missing-token | not-configured | http-error | rejected
$result->forLogging();

'resolver' => [\App\Providers\AppServiceProvider::class, 'assetv'],

Route::view('/turnstile', 'turnstile::bridge')->middleware('throttle:60,1');

use HansDeBoeck\Turnstile\Facades\Turnstile;

$fake = Turnstile::fake();              // alles slaagt
Turnstile::fake(fn ($token) => $token === 'goed');
Turnstile::fakeFailure();               // alles faalt
Turnstile::fakeUnconfigured();          // doet alsof de sleutels ontbreken

$fake->assertVerified('goed');
$fake->assertVerifiedCount(1);
$fake->assertNothingVerified();

Http::fake(['challenges.cloudflare.com/*' => Http::response(['success' => true])]);
js
const gate = window.turnstileGate.create({
    stage: document.querySelector('[data-turnstile-stage]'),
    container: '#mijn-container',
});

gate.token()
    .then((token) => { /* meesturen met je fetch */ })
    .catch((error) => {
        if (gate.isCancelled(error)) return;    // bezoeker brak zelf af
        if (gate.isExpired(error)) return;      // token verliep, opnieuw proberen
        // echte storing
    });

gate.reset();
gate.destroy();   // nodig zodra een dialoog uit de DOM verdwijnt
bash
php artisan vendor:publish --tag=turnstile-config