Laravel 5.3-如何保留会话消息直到用户注销 [英] Laravel 5.3 - How to keep the session message until the users logs out

查看:88
本文介绍了Laravel 5.3-如何保留会话消息直到用户注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注册后,我正在向用户发送欢迎消息.我已经在控制器中修改了trait方法,如下所示:

I am sending a welcome message to user after registration. I have modified the trait method in my controller like so:

public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        Session::set('message','messages.welcome');

        return redirect($this->redirectPath())->with('message', 'messages.welcome');
    }

我也尝试了$request->session()->put('message','messages.welcome');而不是Session::set('message','messages.welcome');,但是它给了我相同的结果.

I have also tried $request->session()->put('message','messages.welcome'); instead of Session::set('message','messages.welcome'); but it gave me the same result.

然后我在这样的视图中显示消息:

And then I am showing the message in the view like this:

@if (session('message'))
   @include(session('message'))
@endif

但是当我刷新视图时,消息消失了,如何保留消息直到用户注销?

But when I refresh the view the messages disappears, how can I keep the messages until the user logs out?

推荐答案

首先写入use语句

use Illuminate\Support\Facades\Session;

然后修改您的方法,例如:

then modify your method like :

public function register(Request $request)
   {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        #$request->session()->put('message', 'messages.welcome'); #this will also work

        Session::put('message', 'messages.welcome');

        return redirect($this->redirectPath());
    }

然后在需要时致电Session::get('message');

这篇关于Laravel 5.3-如何保留会话消息直到用户注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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