Laravel 5.2:在用户登录后做些什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36491035/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:33:51  来源:igfitidea点击:

Laravel 5.2 : Do something after user has logged in?

phplaravelauthenticationevent-handlinglaravel-5.2

提问by u775856

(I'm a beginner of Laravel)

(我是 Laravel 的初学者)

I'm using Laravel 5.2. I have successfully enabled the Authentication; by doing the php artisan make:authand stuffs.

我正在使用Laravel 5.2. 我已成功启用身份验证;通过做php artisan make:auth和东西。

So my login is working.

所以我的登录有效。

Now i need to do something once someone has logged in. For an simple example:

现在我需要在有人登录后做一些事情。举一个简单的例子:

LOGIN:

登录:

  • Once a user has logged in, write a value into Session.
  • For example: $request->session()->put('UserAgent', $ClientUserAgent);
  • 用户登录后,将值写入 Session。
  • 例如: $request->session()->put('UserAgent', $ClientUserAgent);

LOGOUT:

登出:

  • Same thing to do, once a user has logged out, delete the custom Session value.
  • For example: $request->session()->forget('UserAgent');
  • 做同样的事情,一旦用户注销,删除自定义会话值。
  • 例如: $request->session()->forget('UserAgent');

I'm not sure whether there are (things like) hooksor Event Listeners, Event Handlers, or something like that.

我不知道是否有(类似的东西)hooks或者Event ListenersEvent Handlers或者类似的东西。

How can i do it please?

请问我该怎么做?

采纳答案by u775856

Alief's Answerbelow works fine as expected. But as i googled through, using the Event Handlersis probably the more preferred way. (It works like custom hooks).

下面Alief 的回答按预期工作正常。但是当我用谷歌搜索时,使用事件处理程序可能是更受欢迎的方式。(它像自定义钩子一样工作)。

So without any less respects to Alief's Answer below, let me choose --> this Event Handers approachi just found out.

因此,在对下面 Alief 的回答不加任何尊重的情况下,让我选择 -->我刚刚发现的这种事件处理程序方法

Thanks all with regards!

谢谢大家的问候!

回答by kjdion84

For newer versions of Laravel

对于较新版本的 Laravel

If you are only doing something very simple then creating an event handler seems overkill to me. Laravel has an empty method included in the AuthenticatesUsersclass for this purpose.

如果你只是在做一些非常简单的事情,那么创建一个事件处理程序对我来说似乎有点过分。为此,Laravel 在AuthenticatesUsers类中包含了一个空方法。

Just place the following method inside app\Http\Controllers\LoginController(overriding it):

只需将以下方法放入app\Http\Controllers\LoginController(覆盖它):

protected function authenticated(Request $request, $user)
{
    // stuff to do after user logs in
}

回答by Alief

For the post login, you can do that by modifying App/Http/Controllers/Auth/AuthController.php

对于后登录,您可以通过修改来做到这一点 App/Http/Controllers/Auth/AuthController.php

Add authenticated()into that class to override the default one:

添加authenticated()到该类以覆盖默认类:

use Illuminate\Http\Request;

protected function authenticated(Request $request, User $user) {
   // put your thing in here

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

For the logout, add this function into the same class:

对于注销,将此函数添加到同一类中:

use Auth;

protected function getLogout()
{
    Auth::logout();

    // do something here

    return redirect('/');
}

回答by lagbox

You could try setting up event listeners for the Auth events that are fired.

您可以尝试为触发的 Auth 事件设置事件侦听器。

You can setup a listener that listens for Illuminate\Auth\Events\Loginto handle what you need post login and Illuminate\Auth\Events\Logoutfor post logout.

您可以设置一个侦听器来侦听Illuminate\Auth\Events\Login处理登录后和Illuminate\Auth\Events\Logout注销后所需的内容。

Laravel Docs - Authentication - Events

Laravel 文档 - 身份验证 - 事件

回答by Adhiyaman Infotech

If you are testing, with authenticated(Request $request, User $user)method dont use alert inside this method to test, it will not show any result, so better put some insert query or something like that to test this method.

如果您正在测试,使用authenticated(Request $request, User $user)方法不要在此方法中使用警报来测试,它不会显示任何结果,因此最好放置一些插入查询或类似的内容来测试此方法。

回答by Iftikhar uddin

Why not simple check for

为什么不简单检查

if(Auth::check()){
    //your code
}

Make sure you include use Auth;

确保你包括 use Auth;