【问题标题】:Laravel auth not displaying session messageLaravel auth 不显示会话消息
【发布时间】:2018-03-25 03:34:22
【问题描述】:

我正在尝试在 laravel 5.2 中验证用户类型。路线如下:

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

}); 

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 中显示所有错误消息(如在默认身份验证控制器中)?

【问题讨论】:

  • 粘贴您的config/auth.php 代码
  • 您可以添加这样的消息。 $request-&gt;request-&gt;add(['message' =&gt; "Your custome Message"]); 没有进入会话。用Input()重定向回来

标签: php laravel session authentication laravel-5.2


【解决方案1】:

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

$request-&gt;session()-&gt;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']]...

【讨论】:

  • 为什么这个改变会对问题产生影响?
【解决方案2】:

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

那么,除了AuthController 中的重定向之外,您还进行其他重定向吗?也许在中间件中?

你可以尝试一些事情:

  • 添加一些记录每个请求的中间件 (see, how to log every response in Laravel 5.2 framework),以便您可以跟踪是否有其他重定向。

  • session()-&gt;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】:

    据我所知,routes.php 文件中的路由已经默认使用了web 中间件,因此无需再次包装。

    Route::auth() 使用的是默认的 Laravel 身份验证路由,可能您与它们和您的部门登录路由有冲突,所以请尝试删除该行。

    最后,我建议使用session()-&gt;flash('message', 'Invalid credentials'); 而不是-&gt;with()

    【讨论】:

      【解决方案4】:

      另外,使用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')
      

      【讨论】:

        猜你喜欢
        • 2019-10-19
        • 2012-05-28
        • 2016-04-17
        • 2019-07-14
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多