Ajax middleware

I seem to remember that in Laravel 4 there was an ajax filter, this would only allow requests through ajax.

Is there any similar middleware for Laravel 5.

I have a route that receives data from my database via ajax, I want to protect this route so that no one can go to it and see the json string of data.

+6
source share
1 answer

You can use middleware for this.

php artisan make:middleware AllowOnlyAjaxRequests

application / Http / Intermediate / AllowOnlyAjaxRequests.php

 <?php namespace App\Http\Middleware; use Closure; class AllowOnlyAjaxRequests { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if(!$request->ajax()) { // Handle the non-ajax request return response('', 405); } return $next($request); } } 

Add 'ajax' => \App\Http\Middleware\AllowOnlyAjaxRequests::class, to the routeMiddleware array in app/Http/Kernel.php .

Then you can use ajax middleware on your routes.

+26
source

All Articles