Search code examples
phplaravel-4before-filter

laravel 4 allow only logged in user to browse the site


I need to allow only the logged in user to browse the site. After working on filters the only solution I have come across is group filter. But, I have many uri and having group filter will be time-consuming.

I have tried checking if the user is logged in or not in the filters.php App:before but it generates "web page has a redirect loop"

        App::before(function($request)
{
    if(Auth::guest())
    {
        return Redirect::guest('login');
    }
});

I need to allow only logged in user to view the pages of the site, else redirect them to the login page.


Solution

  • Your login filter is in global filter. So, That filter also check before login route. If you don't want to use group filter, just make sure request uri is not same login uri on global filter to prevent redirect loop.

    App::before(function($request)
    {
        if(Auth::guest() && $request->path() != 'login')
        {
            return Redirect::guest('login');
        }
    });