2017-10-13 178 views
4

我正在尝试验证laravel 5.2中的用户类型。这条路线是如下:Laravel auth不显示会话消息

Route::group(['middleware' => ['web']], function(){ 
    Route::auth(); 
    Route::get('/user/department/login', ['as' => 'department_login', 'uses' => 'DepartmentUserAuth\[email protected]']); 
    Route::post('/user/department/login','DepartmentUserAuth\[email protected]'); 

}); 

而且AuthController.php

public function deptLoginPost(Request $request) 
{ 

    $this->validate($request, [ 
     'username'  => 'required', 
     'password'  => 'required', 
    ]); 
    if (auth()->guard('department_user')->attempt(['username' => $request->input('username'), 'password' => $request->input('password')])) 
    { 
     $user = auth()->guard('department_user')->user(); 
     dd($user); 
    }else{ 
     return redirect('/user/department/login')->with('message', 'Invalid credentials'); 
    } 
} 

而且login.blade.php是:

@if(Session::has('message')) 
<div class="row"> 
    <div class="col-lg-12"> 
     <div class="alert {{ Session::get('alert-class', 'alert-info') }}"> 
       <button type="button" class="close" data-dismiss="alert">×</button> 
       {!! Session::get('message') !!} 
     </div> 
     </div> 
</div> 
@endif 

{{ dump(session()->all()) }} 

Session::has('message')是空的,而dump如下所示:

array:3 [▼ 
    "_token" => "BvQ630KrUl78Ngb8d3cst6pAIqenra3ohbYbLzAP" 
    "_previous" => array:1 [▼ 
    "url" => "http://localhost:8080/material/public/user/department/login" 
    ] 
    "flash" => array:2 [▼ 
    "old" => [] 
    "new" => [] 
    ] 
] 

如何在我的login.blade.php中显示所有错误消息(如默认身份验证控制器中的错误消息)?

+0

粘贴' config/auth.php'代码 –

+0

你可以添加这样的消息。 '$ request-> request-> add(['message'=>“Your custome Message”]);'不加入会话。使用Input() –

回答

2

据我所知,这应该是有效的(事实上,我只是在新鲜的Laravel 5.2安装中尝试过这种方法,并且按预期工作)。我唯一的猜测是,with方法不会在会话中“永久”写入消息,而是在闪存数据中(仅在下一个请求周期中可用)。如果出于某种原因,您收到重定向响应,则该响应将消耗闪存数据,并且在您再次到达登录表单时,该消息不再处于会话中。

那么,你在做除AuthController之外的任何重定向吗?也许在一个中间件?

有些事情你可以尝试:

  • 添加一些中间件,它记录每一个请求(see, how to log every response in Laravel 5.2 framework),所以如果有额外的重定向你可以跟踪。

  • session()->reflash();添加到每个请求都运行的中间件。 (如果这项工作,这应该证明我的观点,即闪存数据正在被中间请求消耗)。

  • 更换

    return redirect('/user/department/login') 
        ->with('message', 'Invalid credentials'); 
    

    return redirect() 
        ->back()->with('message', 'Invalid credentials'); 
    
  • 更换(只是暂时的,来测试你可以写,并从会话存储读取)

    return redirect('/user/department/login') 
        ->with('message', 'Invalid credentials'); 
    

    session(['message' => 'Invalid credentials']); 
    return redirect('/user/department/login'); 
    
3

尝试直接上请求的消息设置消息:

$request->session()->flash('message', 'Invalid credentials');

例如:

if (auth()->guard('department_user')->attempt(['username' => $request->input('username'), 'password' => $request->input('password')])) 
{ 
    $user = auth()->guard('department_user')->user(); 
    dd($user); 
}else{ 
    $request->session()->flash('message', 'Invalid credentials'); 
    return redirect()->back(); 
} 

如果仍然没有帮助,看看你的路线,看看是否您的中间件已正确分配。

// either as a string 
Route::group(['middleware' => 'web']... 
// or as a array element 
Route::group(['middlewareGroups' => ['web']]... 
+0

重定向回页首为什么此更改会对问题产生影响? – alepeino

1

据我所知,在routes.php文件路径已经默认使用web中间件,因此没有必要再包裹。

Route::auth()正在使用默认的Laravel身份验证路由,也许您与他们和您的部门登录路由有冲突,因此请尝试删除该行。

去年,我建议使用session()->flash('message', 'Invalid credentials');,而不是->with()

1

此外,使用Session门面:

//display error message 
Session::flash('message', 'Invalid credentials') 

//color the output 
Session::flash('alert-class', 'alert-danger') 

或者,用laracasts/flash,您可以:

//AuthController 
flash('Invalid credentials')->error(); 

//login.blade.php 
@include('flash::message')