登录后Laravel闪烁消息不显示在主路由中
Posted
技术标签:
【中文标题】登录后Laravel闪烁消息不显示在主路由中【英文标题】:Laravel flash message after login does not displayed in home route 【发布时间】:2018-09-24 23:13:18 【问题描述】:首先,我检查了这个问题:Flash message after login laravel,但它并没有解决我的问题。
我正在尝试通过覆盖AuthenticatesUsers
Trait 的authenticated
方法在成功登录后生成会话闪存消息,如下所示:
// in LoginController
...
use Illuminate\Http\Request;
protected function authenticated(Request $request, $user)
$request->session()->flash('status','success');
$request->session()->flash('msg',__('Welcome back :name',['name' => $user->name]));
return redirect('/');
不显示闪存消息。但是,当我设置 return redirect('/anyOtherRoute')
时,它会重定向到该路由,并且 Flash 消息已成功呈现!
我也尝试过覆盖sendLoginResponse
方法而不是authenticated
方法,如下所示:
protected function sendLoginResponse(Request $request)
$request->session()->regenerate();
$this->clearLoginAttempts($request);
$request->session()->flash('status','success');
$request->session()->flash('msg',__('Welcome back :name',['name' => '$user->name']));
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
那里也没有闪信息!最后,我尝试了针对问题的检查答案:Flash message after login laravel,但它也没有任何区别:
use AuthenticatesUsers
redirectPath as laravelRedirectPath;
public function redirectPath()
// Do your logic to flash data to session...
session()->flash('status','success');
session()->flash('msg',__('Welcome back :name',['name' => '$user->name']));
// Return the results of the method we are overriding that we aliased.
return $this->laravelRedirectPath();
以下是我所有的 LoginController 代码,其中包含任何尝试的注释:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/*use AuthenticatesUsers
redirectPath as laravelRedirectPath;
*/
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
//protected $loginPath = 'admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('guest', ['except' => 'logout']);
protected function authenticated(Request $request, $user)
$request->session()->flash('status','success');
$request->session()->flash('msg',__('Welcome back :name',['name' => $user->name]));
return redirect('/job');
/*protected function sendLoginResponse(Request $request)
$request->session()->regenerate();
$this->clearLoginAttempts($request);
$request->session()->flash('status','success');
$request->session()->flash('msg',__('Welcome back :name',['name' => '$user->name']));
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
*/
protected function sendFailedLoginResponse(Request $request)
$errors = [$this->username() => trans('auth.failed')];
if ($request->expectsJson())
return response()->json($errors, 422);
$request->session()->flash('status','danger');
$request->session()->flash('msg',__('Error: Login failed. Incorrect username or password'));
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
public function username()
return 'username';
/* public function redirectPath()
// Do your logic to flash data to session...
session()->flash('status','success');
session()->flash('msg',__('Welcome back :name',['name' => '$user->name']));
// Return the results of the method we are overriding that we aliased.
return $this->laravelRedirectPath();
*/
补充说明:
/
路由被定义为web.php
路由中的链式方法,如下所示:
Route::get('/', ['as' => 'home', function ()
return view('welcome');
])->middleware('auth');
【问题讨论】:
您的视图中是否在路径“/”处有一个位置来呈现 Flash 消息? @MarkSkayff 是的,当然,我在主布局中包含了 Flash 消息元素。此外,在/
路由视图中还呈现了许多其他的flash消息
如果你查看你的网络控制台,重定向到/
后是否还有其他请求?也许发生了另一个重定向,导致您闪现的消息丢失。
@AkenRoberts 很好的通知,我几乎可以肯定这是原因。但是,我无法从网络控制台找出或跟踪重定向,以了解 /
路由的额外重定向发生在哪里!
打开网络管理器并点击持久化日志。如果有另一个请求发生,你必须看到它。
【参考方案1】:
添加“web”中间件以将会话持久化为
Route::group(['middleware' => ['auth', 'web']], function()
Route::get('/', ['as' => 'home', function ()
return view('welcome');
]);
);
【讨论】:
我试过你的建议,但并没有解决问题。 请检查所有路由文件并检查文件末尾是否有空行。【参考方案2】:在我的案例中,laravel 7 可以正常工作:
以下是我的所有LoginController.php
代码,其中包含任何尝试的注释:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
$request->session()->regenerate();
$this->clearLoginAttempts($request);
if ($response = $this->authenticated($request, $this->guard()->user()))
return $response;
return $request->wantsJson()
? new Response('', 204)
: redirect()->back();
// protected function authenticated(Request $request, $user)
//
// $request->session()->flash('flash', 'Welcome!');
//
protected function authenticated(Request $request, $user)
$request->session()->flash('flash','danger');
return redirect()->intended($this->redirectPath());
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('guest')->except('logout');
在刀片文件中:
@if (session('flash'))
<div class="alert alert-success">
<p class="msg"> session('flash') </p>
</div>
@endif
【讨论】:
以上是关于登录后Laravel闪烁消息不显示在主路由中的主要内容,如果未能解决你的问题,请参考以下文章
Laravel:“$ php artisan route:list”在 App\Http\Controllers\API 中不显示路由列表
登录后 Laravel 基本身份验证重定向到 HTTPS 路由
为啥 Api Route 在 laravel 应用程序中不起作用,显示错误(未找到 404 错误)?