Laravel separate filter for normal calls and ajax


I was thinking if we can have separate filters for normal and ajax call in Laravel.

My situation is this. Every request is going through a filter (for a particular controller). If the user is not logged in, filter simply redirects to a not logged page where user is prompted to login again. Well this works fine if a non-logged user enters a url and gets redirected.

But when an AJAX request is sent, the result would contain the not logged page. I have to perform a string search in the result to check for some string that exists on the not logged page.

Is there any other better way to check for AJAX is user is not logged.


You can achieve that with $request->ajax() if Lavavel 5 is the case:

if (\Auth::guest())
    if ($request->ajax())
        return response('Unauthorized.', 401);
        return redirect()->guest('login');

Then, in your javascript's call you just read the response code (401). With Laravel 4 you can use Request::ajax() to verify is the current request is via ajax.