0

$request->ajax(); is not working in laravel 5.5 i want to hendel session redirection for ajax

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Closure;
use Session;
//use Request; 

class IsLogin 
{
    public function handle(Request $request, Closure $next) 
    {
        if($request->ajax())
        {
            return response()->json(['Session_error'=>'Session Expired'], 401);
            //throw new AuthenticationException('Unauthenticated');
        }

    }

DsRaj
  • 2,288
  • 1
  • 16
  • 26
Web 3
  • 1
  • Can you confirm that your middleware is being handled at all? Have you registered the middleware in the `Http/Kernel` class? – nakov May 28 '19 at 07:10
  • @ nakov 'Islogin' => \App\Http\Middleware\IsLogin::class, i already registred in kernel also – Web 3 May 28 '19 at 07:19
  • you can also use `request()->ajax()` and have you apply this middleware at route – Karan May 28 '19 at 07:30
  • @ Knuks if (request()->ajax()) is also not working :( have you apply this middleware at route >> yes. – Web 3 May 28 '19 at 07:33
  • please refer may be helps you https://medium.com/@dennissmink/laravel-ajax-only-middleware-f979a9ab20fe – Karan May 28 '19 at 07:34
  • difference only that `public function handle($request, Closure $next)` – Karan May 28 '19 at 07:35
  • https://medium.com/@dennissmink/laravel-ajax-only-middleware-f979a9ab20fe >>Already try but not working – Web 3 May 28 '19 at 07:38
  • @Web3 try after remove `Request` in handle function and you can see existing middleware for example – Karan May 28 '19 at 07:40
  • Hi, hope this can help you. https://stackoverflow.com/questions/29231587/laravel-check-if-ajax-request – emjr May 28 '19 at 07:41
  • @emjr $request->ajax() is not working. is not able to parse the ajax request so i can't identify ajax request. – Web 3 May 28 '19 at 08:01

0 Answers0