public function testRedirect() { $url = 'http://google.com/'; $code = 302; $response = new Response(); $response->redirect($url, $code); $this->assertSame($code, $response->code()); $this->assertSame($url, $response->headers()->get('location')); $this->assertTrue($response->isLocked()); }
/** * Alias to set a response code, lock the response, and halt the route matching/dispatching * * @param int $code Optional HTTP status code to send * @throws DispatchHaltedException To halt/skip the current dispatch loop * @access public * @return void */ public function abort($code = null) { if (null !== $code) { $this->response->code($code); } // Disallow further response modification $this->response->lock(); throw new DispatchHaltedException(); }
/** * Handles an HTTP error exception through our HTTP error callbacks * * @param HttpExceptionInterface $http_exception The exception that occurred * @param RouteCollection $matched The collection of routes that were matched in dispatch * @param array $methods_matched The HTTP methods that were matched in dispatch * @access protected * @return void */ protected function httpError(HttpExceptionInterface $http_exception, RouteCollection $matched, $methods_matched) { if (!$this->response->isLocked()) { $this->response->code($http_exception->getCode()); } if (count($this->httpErrorCallbacks) > 0) { foreach (array_reverse($this->httpErrorCallbacks) as $callback) { if ($callback instanceof Route) { $this->handleRouteCallback($callback, $matched, $methods_matched); } elseif (is_callable($callback)) { if (is_string($callback)) { $callback($http_exception->getCode(), $this, $matched, $methods_matched, $http_exception); } else { call_user_func($callback, $http_exception->getCode(), $this, $matched, $methods_matched, $http_exception); } } } } // Lock our response, since we probably don't want // anything else messing with our error code/body $this->response->lock(); }
/** * Routes an exception through the error callbacks * * @param Exception $err The exception that occurred * @access protected * @return void */ protected function error(Exception $err) { $type = get_class($err); $msg = $err->getMessage(); if (count($this->errorCallbacks) > 0) { foreach (array_reverse($this->errorCallbacks) as $callback) { if (is_callable($callback)) { if (is_string($callback)) { if ($callback($this, $msg, $type, $err)) { return; } } else { if (call_user_func($callback, $this, $msg, $type, $err)) { return; } } } else { if (null !== $this->service && null !== $this->response) { $this->service->flash($err); $this->response->redirect($callback); } } } } else { $this->response->code(500); throw new UnhandledException($err); } }