问题
I'm using Laravel 5.2. I want to check user session in routes file, so that if session is set user can visit dashboard otherwise redirect to login page.
I have used following code for this but it's not working. It's not giving any error and not redirecting him to login page. anyhow if I write same code in controller functioin, it works fine.
Route::group(['middleware' => ['web']], function () {
Route::get('dashboard/index', ['uses' => 'DashboardController@index'], function() {
$value = $request->session()->get('name', 'not_loggin');
if ($value == 'not_loggin') {
return redirect('/user/login');
}
});
});
it also didn't worked if I write it in constructor.
回答1:
You should use the auth
middleware:
Route::get('dashboard/index', [
'middleware' => 'auth',
'uses' => 'DashboardController@index'
]);
来源:https://stackoverflow.com/questions/35775014/how-to-check-if-user-is-logged-in-by-his-session-in-route-and-then-call-controll