Php 登录laravel后的Flash消息
我正在使用Laravel5.4和预构建的auth库 我想有一个甜警报(或弹出窗口)出现时,用户成功登录,但我找不到的逻辑是重定向发生的地方 因此: 用户成功登录 转发到home controller(需要在代码中找到它并附加一条flash消息或其他内容?) 在我的视图中,检测是否有flash消息,并在内存中创建一个flash消息,这样我就可以使用Javascript查询它并显示一个警报框 编辑: 我已经有了这套,很好:Php 登录laravel后的Flash消息,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在使用Laravel5.4和预构建的auth库 我想有一个甜警报(或弹出窗口)出现时,用户成功登录,但我找不到的逻辑是重定向发生的地方 因此: 用户成功登录 转发到home controller(需要在代码中找到它并附加一条flash消息或其他内容?) 在我的视图中,检测是否有flash消息,并在内存中创建一个flash消息,这样我就可以使用Javascript查询它并显示一个警报框 编辑: 我已经有了这套,很好: use AuthenticatesUsers; /** * Where
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
但我需要找到引用的位置?并添加一条flash消息
谢谢
app/Http/Controllers/Auth/LoginController.php
以引用HomeController路由并刷新消息:使用会话代码>
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());
}
然后,在您的视图中添加以下内容:
@if(session()->has('flash\u notification.success'){!!session('flash\u notification.success')
@endif
最后,确保包含css的引导
如果您想让用户使用ajax登录,并在成功登录时显示一条flash消息,然后重新加载/重定向,则必须对Auth\login控制器进行一些更改
将这两种方法添加到Auth\Login Controller
,第一种方法将在成功登录时发送响应,而第二种方法将生成错误消息
protected function sendLoginResponse(Request $request) {
$request->session()->regenerate();
$this->clearLoginAttempts($request);
if ($request->ajax()) {
return response()->json($this->guard()->user(), 200);
}
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
protected function sendFailedLoginResponse(Request $request)
{
if ($request->ajax()) {
return response()->json([
'error' => Lang::get('auth.failed')
], 401);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors([
$this->username() => Lang::get('auth.failed'),
]);
}
然后使用jQuery(如果您正在使用它),就可以做到这一点
var fdata = $("#loginForm").serialize();
var furl = $("#loginForm").attr( "action" );
$.ajax({
url: furl,
type: "POST",
data: fdata,
success: function(data){
console.log("Login Success");
location.reload();
},
error: function(data){
var errors = data.responseJSON;
if(errors.error){
console.log(errors.error);
}
}
});
当然,若您希望在服务器上设置自定义错误/闪存消息,可以将其添加到响应json中
希望有帮助。该属性在方法redirectPath()
中的traitRedirectsUsers
中引用。该特征用于特征验证用户
,该特征正在登录控制器
中使用
话虽如此,您需要做的是相当简单的。在登录控制器中,我们需要重写trait中的方法,以便它也将您的消息闪烁到会话。但是,因为该方法位于trait中,我们不只是想覆盖它,我们仍然想在trait中使用该方法,所以我们首先需要将其别名为其他内容
use AuthenticatesUsers {
redirectPath as laravelRedirectPath;
};
现在,我们可以安全地重写此方法以将数据闪存到会话
/**
* 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
事件,并在处理程序中刷新其中的数据。也许上面代码的最后一行应该是return redirect()->designed($this->redirectTo)代码>