如何在Laravel 5.8中登录时执行添加操作?
在Laravel 5.8中,Auth\LoginController只是:如何在Laravel 5.8中登录时执行添加操作?,laravel,Laravel,在Laravel 5.8中,Auth\LoginController只是: class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |------------------------------------------------------
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 = '/my-team';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
在登录时,我想为用户执行一些自定义操作,但我不知道在哪里可以放置此代码。文档似乎没有帮助
是否有一种方法我可以用自己的方法覆盖/扩展
Laravel 5.2以前在
Auth
控制器中有一个login()
方法,我可以在其中编写额外的代码。有两个功能由AuthenticatesUsers提供。您可以在登录控制器中自定义这些
登录表单
public function showLoginForm()
{
return view('auth.login');
}
处理登录
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
您可以将这两个函数放置在登录控制器中,并根据需要进行更改。您可以覆盖LoginController中的login()
方法。正如您所说,在Laravel 5.8中,login()
方法不存在,但您可以自己定义它。新定义的login()。以下是Laracasts的一个片段:
公共功能登录(请求$Request)
{
$this->validateLogin($request);
如果($this->hastoomanyLogin尝试($request)){
$this->fireLockoutEvent($request);
返回$this->sendLockoutResponse($request);
}
如果(身份验证::尝试(['email'=>$request->email,'password'=>$request->password,'is_activated'=>1])){
//return redirect()->designed('dashboard');
}否则{
$this->incrementLoginAttents($request);
返回响应()->json([
'错误'=>'此帐户未激活。'
], 401);
}
$this->incrementLoginAttents($request);
返回$this->sendFailedLoginResponse($request);
}
只要仔细检查一下,看看那里都做了些什么。简而言之,您可以在用户登录之前或之后修改login()
方法来执行任何您想执行的操作。只要转到illumb\Foundation\Auth\AuthenticatesUsers
特征所在的位置,您就会找到所有您想要的方法
它位于:
vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers
或者,您可以在LoginController中覆盖它以执行任何您想要的操作添加自定义操作的最佳位置是覆盖LoginController中的已验证的
方法
protected function authenticated(Request $request, $user)
{
// Your code
}
登录方法确实存在,它在AuthenticatesUsers
trait@DerDjamel但是您不能修改供应商/laravel/framework/src/illumb/Foundation/Auth/AuthenticatesUsers
,因为它将被覆盖。我不是说您是否可以覆盖它,我说的是它是否存在not@DerDjamelOP试图在LoginController
中找到login()
方法,因此解决方案是定义登录方法,因为它没有在LoginController
中定义。是的,它确实存在于AuthenticatedUsers
中,但他不应该修改该文件。出于这个原因,我建议重写login()
方法,这是实现他想要的标准方法:)在某些情况下,您也是正确的。