Redirect to Login if user is not logged in to Laravel

I'm new to laravel

I have code in my __construct controller as

if(Auth::check())
{
    return View::make('view_page');
}

return Redirect::route('login')->withInput()->with('errmessage', 'Please Login to access restricted area.');

works fine but i want to. its really annoying to put these encodings in each controller, so I want to put this Verify Auth and redirect to the login page in one place, maybe in router.phpor filters.php.

I read several forum posts as well as in stackoverflowand added the code to filters.phpas shown below, but this does not work either.

Route::filter('auth', function() {
    if (Auth::guest())
    return Redirect::guest('login');
});

Please help me solve this problem.

+4
source share
1 answer

Laravel 5.4

auth.

Route::group(['middleware' => ['auth']], function() {
    // your routes
});

:

Route::get('profile', function () {
    // Only authenticated users may enter...
})->middleware('auth');

Laravel

Laravel 4 ( )

laravel. . auth filters.php. . :

Route::group(array('before' => 'auth'), function(){
    // your routes

    Route::get('/', 'HomeController@index');
});

:

Route::get('/', array('before' => 'auth', 'uses' => 'HomeController@index'));

URL- , filters.php .

+19

All Articles