auth = $auth; $this->registrar = $registrar; //注册auth.attemp事件 //加入验证码的验证 $this->auth->attempting(function()use($request){ $phrase = \Session::get('phrase'); if($request->input('phrase') != $phrase){ throw new HttpResponseException( redirect('/auth/login')->withInput($request->input())->withErrors(['phrase'=>'验证码错误']) ); } }); $this->middleware('guest', ['except' => 'getLogout']); }}
需要特别说明的是HttpResponseException这个异常,这个异常接收一个Response作为参数,在Illuminate\Routing\Route的run方法中会捕获这个异常并返回设置的Response,所以我们可以通过抛出这个异常来终止我们的应用程序并跳转
public function run(Request $request) { $this->container = $this->container ?: new Container; try { if ( ! is_string($this->action['uses'])) return $this->runCallable($request); if ($this->customDispatcherIsBound()) return $this->runWithCustomDispatcher($request); return $this->runController($request); } catch (HttpResponseException $e) { return $e->getResponse(); } }