-
app/Validators/ReCaptcha.php
Open in GitHubuse GuzzleHttp\Client; class ReCaptcha { public function validate($attribute, $value, $parameters, $validator) { $client = new Client; $response = $client->post( 'https://www.google.com/recaptcha/api/siteverify', [ 'form_params' => [ 'secret' => config('services.recaptcha.secret'), 'response' => $value ] ] ); $body = json_decode((string)$response->getBody()); return $body->success; } }
-
resources/views/contact.blade.php
Open in GitHub// @section('css') <script src='https://www.google.com/recaptcha/api.js'></script> @endsection // @if(config('services.recaptcha.key')) <div class="form-group"> <div class="g-recaptcha" data-sitekey="{{config('services.recaptcha.key')}}"> </div> @error('g-recaptcha-response') <span class="invalid-feedback mt-3" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> @endif // <script src="https://maps.googleapis.com/maps/api/js?key={{ env('GOOGLE_MAPS_KEY') }}&callback=initMap"> </script> </body> </html> @endsection
-
app/Providers/AppServiceProvider.php
Open in GitHubuse Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Validator; class AppServiceProvider extends ServiceProvider { // public function boot() { // Validator::extend('recaptcha', 'App\\Validators\\ReCaptcha@validate'); } }
-
app/Http/Controllers/FrontendController.php
Open in GitHubclass FrontendController extends Controller { // public function contactStore(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required', 'subject' => 'required', 'message' => 'required', 'g-recaptcha-response' => config('services.recaptcha.key') ? 'required|recaptcha' : 'nullable', ]); // } // }