登录后的Laravel flash消息未显示在归属路由中

时间:2018-04-14 21:54:01

标签: php laravel authentication laravel-5 laravel-5.4

首先,我查看了这个问题:Flash message after login laravel,但它并没有解决我的问题。

我尝试在成功登录后生成会话Flash消息,通过覆盖authenticated Trait的AuthenticatesUsers方法,如下所示:

// 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');

2 个答案:

答案 0 :(得分:0)

添加&#34; web&#34;中间件以持续会话

Route::group(['middleware' => ['auth', 'web']], function() {
    Route::get('/', ['as' => 'home', function () {
        return view('welcome');
    }]);
});

答案 1 :(得分:0)

以我的情况在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