我希望只有当用户登录时,我的应用程序才会转到admin_panel.blade.php。我不想直接去那里。所以我实现了中间件和会话,但它不起作用,因为如果我直接转到"admin_panel",它不会限制我。无论是否使用登录信息,它都允许我转到admin_panel。
Web.php
Route::get('/admin_log', function () {
return view('Admin.admin_login');
});
Route::group(['middleware'=>'session_auth'],function(){
Route::get('/admin_panel','LoginController@admin_panel');
登录控制器
public function admin_panel(){
return view('Admin.admin_panel');
}
public function admin_login(Request $req){
$login=AdminLogin::first();
if ($login->Admin_Name==$req->admin_name && $login->Admin_Password==$req->admin_password ){
$req->session()->put('session_name',$req->admin_name);
return redirect('admin_panel');
}
else{
return redirect('admin_log')->with('error','Invalid UserName or Password!');
}
}
中间件
public function handle($request, Closure $next)
{
if(is_null($request->session()->get('session_name'))){
return redirect('/admin_log');
}
return $next($request);
}
Kernal.php
protected $routeMiddleware = [
'auth' => AppHttpMiddlewareAuthenticate::class,
'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class,
'can' => IlluminateAuthMiddlewareAuthorize::class,
'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
'signed' => IlluminateRoutingMiddlewareValidateSignature::class,
'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
'verified' => IlluminateAuthMiddlewareEnsureEmailIsVerified::class,
'session_auth' => AppHttpMiddlewaresession_auth::class,
];
当您转到/admin_panel查看其包含的内容时,您是否在中间件的handle((函数中尝试了dd($request->session(((?
如果你没有被重定向到/admin_log,这可能意味着你没有输入你的条件,并且你的session_name存在。
希望这能有所帮助。
问候