| 
5 | 5 | [](https://github.com/ryangjchandler/laravel-cloudflare-turnstile/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)  | 
6 | 6 | [](https://packagist.org/packages/ryangjchandler/laravel-cloudflare-turnstile)  | 
7 | 7 | 
 
  | 
8 |  | -This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.  | 
 | 8 | +This packages provides helper for setting up and validating Cloudflare Turnstile CAPTCHA responses.  | 
9 | 9 | 
 
  | 
10 |  | -## Support us  | 
 | 10 | +## Installation  | 
11 | 11 | 
 
  | 
12 |  | -[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-cloudflare-turnstile.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-cloudflare-turnstile)  | 
 | 12 | +You can install the package via Composer:  | 
13 | 13 | 
 
  | 
14 |  | -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).  | 
 | 14 | +```bash  | 
 | 15 | +composer require ryangjchandler/laravel-cloudflare-turnstile  | 
 | 16 | +```  | 
15 | 17 | 
 
  | 
16 |  | -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).  | 
 | 18 | +You should then add the following configuration values to your `config/services.php` file:  | 
17 | 19 | 
 
  | 
18 |  | -## Installation  | 
 | 20 | +```php  | 
 | 21 | +return [  | 
19 | 22 | 
 
  | 
20 |  | -You can install the package via composer:  | 
 | 23 | +    // ...,  | 
21 | 24 | 
 
  | 
22 |  | -```bash  | 
23 |  | -composer require ryangjchandler/laravel-cloudflare-turnstile  | 
 | 25 | +    'turnstile' => [  | 
 | 26 | +        'key' => env('TURNSTILE_SITE_KEY'),  | 
 | 27 | +        'secret' => env('TURNSTILE_SECRET_KEY'),  | 
 | 28 | +    ],  | 
 | 29 | + | 
 | 30 | +];  | 
24 | 31 | ```  | 
25 | 32 | 
 
  | 
26 |  | -You can publish and run the migrations with:  | 
 | 33 | +Visit Cloudflare to create your site key and secret key and add them to your `.env` file.  | 
27 | 34 | 
 
  | 
28 |  | -```bash  | 
29 |  | -php artisan vendor:publish --tag="laravel-cloudflare-turnstile-migrations"  | 
30 |  | -php artisan migrate  | 
 | 35 | +```  | 
 | 36 | +TURNSTILE_SITE_KEY="1x00000000000000000000AA"  | 
 | 37 | +TURNSTILE_SECRET_KEY="2x0000000000000000000000000000000AA"  | 
31 | 38 | ```  | 
32 | 39 | 
 
  | 
33 |  | -You can publish the config file with:  | 
 | 40 | +## Usage  | 
34 | 41 | 
 
  | 
35 |  | -```bash  | 
36 |  | -php artisan vendor:publish --tag="laravel-cloudflare-turnstile-config"  | 
 | 42 | +In your layout file, include the Turnstile scripts using the `@turnstileScripts` Blade directive. This should be added to the `<head>` of your document.  | 
 | 43 | + | 
 | 44 | +```blade  | 
 | 45 | +<html>  | 
 | 46 | +    <head>  | 
 | 47 | +        @turnstileScripts()  | 
 | 48 | +    </head>  | 
 | 49 | +    <body>  | 
 | 50 | +        {{ $slot }}  | 
 | 51 | +    </body>  | 
 | 52 | +</html>  | 
37 | 53 | ```  | 
38 | 54 | 
 
  | 
39 |  | -This is the contents of the published config file:  | 
 | 55 | +Once that's done, you can use the `@turnstile` directive in `<form>` to output the appropriate markup with your site key configured.  | 
40 | 56 | 
 
  | 
41 |  | -```php  | 
42 |  | -return [  | 
43 |  | -];  | 
 | 57 | +```blade  | 
 | 58 | +<form action="/" method="POST">  | 
 | 59 | +    @turnstile()  | 
 | 60 | +
  | 
 | 61 | +    <button>  | 
 | 62 | +        Submit  | 
 | 63 | +    </button>  | 
 | 64 | +</form>  | 
44 | 65 | ```  | 
45 | 66 | 
 
  | 
46 |  | -Optionally, you can publish the views using  | 
 | 67 | +On the server, use the provided validation rule to validate the CAPTCHA response.  | 
47 | 68 | 
 
  | 
48 |  | -```bash  | 
49 |  | -php artisan vendor:publish --tag="laravel-cloudflare-turnstile-views"  | 
 | 69 | +```php  | 
 | 70 | +use Illuminate\Validation\Rule;  | 
 | 71 | + | 
 | 72 | +public function submit(Request $request)  | 
 | 73 | +{  | 
 | 74 | +    $request->validate([  | 
 | 75 | +        'cf-turnstile-response' => ['required', Rule::turnstile()],  | 
 | 76 | +    ]);  | 
 | 77 | +}  | 
50 | 78 | ```  | 
51 | 79 | 
 
  | 
52 |  | -## Usage  | 
 | 80 | +If you prefer to not use a macro, you can resolve an instance of the rule from the container via dependency injection or the `app()` helper.  | 
 | 81 | + | 
 | 82 | +```php  | 
 | 83 | +use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;  | 
 | 84 | + | 
 | 85 | +public function submit(Request $request, Turnstile $turnstile)  | 
 | 86 | +{  | 
 | 87 | +    $request->validate([  | 
 | 88 | +        'cf-turnstile-response' => ['required', $turnstile],  | 
 | 89 | +    ]);  | 
 | 90 | +}  | 
 | 91 | +```  | 
53 | 92 | 
 
  | 
54 | 93 | ```php  | 
55 |  | -$laravelCloudflareTurnstile = new RyanChandler\LaravelCloudflareTurnstile();  | 
56 |  | -echo $laravelCloudflareTurnstile->echoPhrase('Hello, RyanChandler!');  | 
 | 94 | +use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;  | 
 | 95 | + | 
 | 96 | +public function submit(Request $request)  | 
 | 97 | +{  | 
 | 98 | +    $request->validate([  | 
 | 99 | +        'cf-turnstile-response' => ['required', app(Turnstile::class)],  | 
 | 100 | +    ]);  | 
 | 101 | +}  | 
57 | 102 | ```  | 
58 | 103 | 
 
  | 
59 | 104 | ## Testing  | 
 | 
0 commit comments