登录 laravel 后的闪信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42119645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flash message after login laravel
提问by Alex
I'm using Laravel 5.4 with the pre-built auth library.
我将 Laravel 5.4 与预构建的 auth 库一起使用。
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.
当用户成功登录时,我想有一个甜蜜的警报(或弹出窗口)出现,但我找不到逻辑在哪里发生重定向。
So:
所以:
User logins successfully
用户登录成功
Gets forwarded to home controller (need to find this in the code and attach a flash message or something else?)
被转发到家庭控制器(需要在代码中找到它并附上一个闪信或其他东西?)
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.
在我看来,检测是否有闪现消息,如果内存中有闪现消息,那么我就可以使用 Javascript 查询它并显示一个警报框。
Edit:
编辑:
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
但我需要找到引用它的地方?并添加一条快闪信息
Thanks
谢谢
回答by Paras
- Edit your
app/Http/Controllers/Auth/LoginController.php
to reference the HomeController route and flash your message:
- 编辑您
app/Http/Controllers/Auth/LoginController.php
以引用 HomeController 路由并闪烁您的消息:
use Session;
use Session;
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());
}
Then, in your view add the following:
@if (session()->has('flash_notification.success')) <div class="alert alert-success">{!! session('flash_notification.success') !!}</div> @endif
Finally, make sure you include bootstrap for the css
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
然后,在您的视图中添加以下内容:
@if (session()->has('flash_notification.success')) <div class="alert alert-success">{!! session('flash_notification.success') !!}</div> @endif
最后,确保为 css 包含引导程序
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
回答by user1669496
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
.
该属性RedirectsUsers
在方法的特征中被引用redirectPath()
。该 trait 用于AuthenticatesUsers
正在使用的 trait中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.
话虽如此,您需要做的事情相当简单。在登录控制器中,我们需要覆盖特征中的方法,以便它也将您的消息闪烁到会话中。但是因为该方法在 trait 中,我们不只是想覆盖它,我们仍然想在 trait 中使用该方法,所以我们首先需要将它别名为其他东西。
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();
}
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.
如果你不想经历这个,你可以做的另一件事是监听auth.login
事件并在你的处理程序中,在那里刷入数据。
回答by Rohan Shewale
If you want to have user sign in with ajax and have a flash message on successfully login and then reload/redirect, you will have to make some changes to the Auth\Login Controller
如果您想让用户使用 ajax 登录并在成功登录时显示闪现消息,然后重新加载/重定向,则必须对 Auth\Login Controller
Add this two methods to
Auth\Login Controller
, this first one will send response on successful login, while the second one will produce error message.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'), ]); }
Then with jQuery (if you are using it), you can do this
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); } } });
将这两个方法添加到
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); } } });
And of course, if you wish to set custom error/flash message on server you can add it to response json.
当然,如果您希望在服务器上设置自定义错误/闪存消息,您可以将其添加到响应 json 中。
Hope it helps.
希望能帮助到你。