登录laravel后出现Flash消息

Ale*_*lex 3 php laravel laravel-5

我将Laravel 5.4与预建的身份验证库一起使用。

当用户成功登录时,我想发出一个甜蜜的警报(或弹出窗口),但我找不到重定向发生的逻辑位置。

所以:

用户登录成功

被转发到家庭控制器(需要在代码中找到它并附加即显消息还是其他?)

在我看来,检测内存中是否有即时消息,并确定内存中是否有即时消息,因此我可以使用Javascript查询并显示一个警告框。

编辑:

我已经设置好了,很好:

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/';
Run Code Online (Sandbox Code Playgroud)

但是我需要找到引用的地方?并添加即时消息

谢谢

Par*_*ras 5

  1. 编辑您的代码app/Http/Controllers/Auth/LoginController.php以引用HomeController路由并刷新消息:

use Session;

protected $redirectTo = '/home'; // add your route here

/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
 protected function authenticated(Request $request, $user)
 {
    $request->session()->flash('flash_notification.success', 'Congratulations, you have cracked the code!');
    return redirect()->intended($this->redirectPath());
 }
Run Code Online (Sandbox Code Playgroud)
  1. 然后,在您的视图中添加以下内容:

    @if (session()->has('flash_notification.success')) <div class="alert alert-success">{!! session('flash_notification.success') !!}</div> @endif

  2. 最后,确保您包括CSS的引导程序 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">


use*_*496 5

RedirectsUsers该方法的特征中引用了该属性redirectPath()。该性状在性状中使用AuthenticatesUsers其在被使用LoginController

话虽如此,您需要做的非常简单。在登录控制器中,我们需要覆盖特征中的方法,以便它也将您的消息闪烁到会话。但是由于该方法位于特征中,我们不仅要覆盖它,我们仍然想在特征中使用该方法,因此我们首先需要将其别名。

use AuthenticatesUsers {
    redirectPath as laravelRedirectPath;
};
Run Code Online (Sandbox Code Playgroud)

现在,我们可以安全地重写此方法,以将数据刷新到会话。

/**
 * Get the post register / login redirect path.
 *
 * @return string
 */
public function redirectPath()
{
    // Do your logic to flash data to session...
    session()->flash('message', 'your message');

    // Return the results of the method we are overriding that we aliased.
    return $this->laravelRedirectPath();
}
Run Code Online (Sandbox Code Playgroud)

如果您不想执行此操作,则可以做的另一件事是侦听该auth.login事件,然后在处理程序中刷新其中的数据。