这是一个不同的问题,因为它与我的代码没有直接关系,而是与another question中导致问题的代码直接相关。
在挖掘与其他问题相关的代码时,我发现了一个我不理解的奇怪的return
语句。在继续我的问题之前,请查看laravel/framework
代码中的这两个代码段。
特质\Illuminate\Foundation\Auth\AuthenticatesUsers:
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
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);
}
特质\Illuminate\Foundation\Auth\ThrottlesLogins:
/**
* Redirect the user after determining they are locked out.
*
* @param \Illuminate\Http\Request $request
* @return void
* @throws \Illuminate\Validation\ValidationException
*/
protected function sendLockoutResponse(Request $request)
{
$seconds = $this->limiter()->availableIn(
$this->throttleKey($request)
);
throw ValidationException::withMessages([
$this->username() => [Lang::get('auth.throttle', ['seconds' => $seconds])],
])->status(429);
}
在AuthenticatesUsers
特征中,检查是否为给定用户进行了太多的认证尝试。如果是,则触发事件并返回锁定响应。到目前为止一切都很好。
我不理解的是return
前面的$this->sendLockoutResponse($request)
语句。给定方法确实总是抛出一个异常并且什么都不返回(好吧,它会返回void
,但它没有,因为它总是throws
)。
那么这里return
语句的目的是什么?这是否为读者提示此时login()
被取消或者这是一些特殊的语法我以前从未听说过?