PHPackages                             nhantamz/laravel-turnstile - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. nhantamz/laravel-turnstile

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

nhantamz/laravel-turnstile
==========================

Cloudflare Turnstile for Laravel

v1.0.1(11mo ago)018MITPHPPHP &gt;=5.5.5

Since Jul 3Pushed 11mo agoCompare

[ Source](https://github.com/nhantamz/laravel-turnstile)[ Packagist](https://packagist.org/packages/nhantamz/laravel-turnstile)[ RSS](/packages/nhantamz-laravel-turnstile/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

Introduction
============

[](#introduction)

The package is based on  but for Cloudflare Turnstile. If you use HCaptcha, please use Scyllaly's package.

Thank you Scyllaly!

Installation
------------

[](#installation)

```
composer require nhantamz/laravel-turnstile

```

Laravel 5 and above
-------------------

[](#laravel-5-and-above)

### Setup

[](#setup)

In `app/config/app.php` add the following :

Step 1: The ServiceProvider to the providers array :

```
Nhantamz\Turnstile\TurnstileServiceProvider::class,
```

Step 2: The class alias to the aliases array :

```
'Turnstile' => Nhantamz\Turnstile\Facades\Turnstile::class,
```

Step 3: Publish the config file (turnstile.php)

```
php artisan vendor:publish --provider="Nhantamz\Turnstile\TurnstileServiceProvider"
```

### Configuration

[](#configuration)

Add `TURNSTILE_SECRET`, `TURNSTILE_SITEKEY` and `TURNSTILE_ENABLED` in **.env** file :

```
TURNSTILE_ENABLED=true
TURNSTILE_SITEKEY=site-key
TURNSTILE_SECRET=secret-key

```

(You can obtain them by following the instructions from [Official Developer Guide](https://developers.cloudflare.com/turnstile/get-started/))

### Usage

[](#usage)

#### Init js source

[](#init-js-source)

With default options :

```
 {!! Turnstile::renderJs() !!}
```

With callback :

```
 {!! Turnstile::renderJs(true, 'TurnstileCallback') !!}
```

#### Display Turnstile

[](#display-turnstile)

Default widget :

```
{!! Turnstile::display() !!}
```

With [custom attributes](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/) (You can set language, theme, size, appearance in config file or set directly as attributes.) :

```
{!! Turnstile::display(['class' => 'css-class', 'data-theme' => 'light']) !!}
```

#### Validation

[](#validation)

There are two ways to apply Turnstile validation to your form:

#### 1. Basic Approach

[](#1-basic-approach)

This method always applies the Turnstile validation rule.

```
$validate = Validator::make(Input::all(), [
    'cf-turnstile-response' => 'required|Turnstile'
]);
```

In this approach, the `cf-turnstile-response` field is required and validated using the `Turnstile` rule without any conditions.

#### 2. Conditional Approach

[](#2-conditional-approach)

This method applies the Turnstile validation rule only if the `TURNSTILE_ENABLED` environment variable is set to `true`.

```
$isTurnstileEnabled = env('TURNSTILE_ENABLED');
$rules = [
    // Other validation rules...
];

if ($isTurnstileEnabled) {
    $rules['cf-turnstile-response'] = 'required|Turnstile';
}

$request->validate($rules);
```

In this approach, the `cf-turnstile-response` field will be required and validated using the `Turnstile` rule only when `TURNSTILE_ENABLED` is set to `true`. This adds flexibility to your validation logic, allowing you to enable or disable Turnstile validation as needed.

##### Custom Validation Message

[](#custom-validation-message)

Add the following values to the `custom` array in the `validation` language file :

```
'custom' => [
    'cf-turnstile-response' => [
        'required' => 'Please verify that you are not a robot.',
        'turnstile' => 'Captcha error! try again later or contact site admin.',
    ],
],
```

Then check for captcha errors in the `Form` :

```
@if ($errors->has('cf-turnstile-response'))

        {{ $errors->first('cf-turnstile-response') }}

@endif
```

Without Laravel
---------------

[](#without-laravel)

Checkout example below:

```
