Laravel 5.2:在用户登录/注销和注册后显示会话flash消息

时间:2016-07-22 15:54:57

标签: php flash authentication laravel-5 notifications

使用运行php artisan make:auth后生成的默认设置,我现在想知道如何控制用户登录时需要显示的Flash消息。

到目前为止,我已尝试将此代码添加到默认AuthController

 public function authenticated($request, $user)
    {
        flash('Welcome back ' . $user->username . ', you have been logged in');
    }

但是这会导致我的Chrome浏览器出现太多重定向错误。如何在不创建自定义LoginController或覆盖login内的完整AuthController方法的情况下刷新自定义消息?

1 个答案:

答案 0 :(得分:1)

我必须在AuthController中覆盖以下方法:

/**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $successmessage = 'you are now successfully registered!';
        flash()->overlay('Yes', $successmessage, 'success');
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

    }

    /**
     * Log the user out of the application.
     *
     * @return \Illuminate\Http\Response
     */
    public function logout()
    {
        Auth::logout();
        flash()->info('Bye', 'You have been successfully logged out!');
        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
    }

    /**
     * Function called after user logs in
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function authenticated() {
        $successmessage = 'Hej '.Auth::user()->name.', you are logged in!';
        flash()->success('Hello', $successmessage);
        return redirect()->intended($this->redirectPath());
    }

那就是它!请注意,我使用的是自定义闪存设置。您可以使用默认值或您自己的