登录laravel后出现Flash消息 [英] Flash message after login laravel

查看:46
本文介绍了登录laravel后出现Flash消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Laravel 5.4 with the pre-built auth library.

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

I would like to have a sweet alert (or popup) come up when a user sucessfully logins but I can't find where the logic is where the redirect to happens.

所以:

用户成功登录

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

Gets forwarded to home controller (need to find this in the code and attach a flash message or something else?)

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

In my view detect if there's a flash message and make a if there's a flash message in memory so I can then query it with Javascript and show an alert box.

我已经设置好了,没关系:

I already have this set and it's fine:

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/';

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

but I need to find where this is referenced? and add a flash message

谢谢

推荐答案

该属性在方法redirectPath()中的特征RedirectsUsers中引用.该特征在LoginController中使用的特征AuthenticatesUsers中使用.

That property is referenced in a trait RedirectsUsers in the method redirectPath(). That trait is used in the trait AuthenticatesUsers which is being used in the LoginController.

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

With that said, what you need to do is fairly simple. In the login controller, we need to override the method in the trait so that it also flashes your message to session. But because that method is in a trait, we don't just want to override it, we still want to use the method in the trait so we first need to alias it as something else.

use AuthenticatesUsers {
    redirectPath as laravelRedirectPath;
};

现在,我们可以安全地覆盖此方法,以将数据刷新到会话中.

Now we can safely override this method to flash data to session.

/**
 * 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();
}

如果您不想执行此操作,则可以做的另一件事是监听auth.login事件,并在处理程序中将数据刷新在那里.

If you don't wish to go through this, another thing you can do is listen for the auth.login event and in your handler, flash the data there.

这篇关于登录laravel后出现Flash消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆