diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 99ceb0ca1815..2d4b1c819bd1 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -38,7 +38,6 @@ namespace CodeIgniter; use Closure; -use CodeIgniter\Filters\Exceptions\FilterException; use CodeIgniter\HTTP\DownloadResponse; use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\Request; @@ -50,6 +49,7 @@ use CodeIgniter\Events\Events; use CodeIgniter\HTTP\Response; use CodeIgniter\HTTP\CLIRequest; +use CodeIgniter\Router\Exceptions\RedirectException; use CodeIgniter\Router\RouteCollectionInterface; use CodeIgniter\Exceptions\PageNotFoundException; use Exception; @@ -209,7 +209,7 @@ public function initialize() * @param boolean $returnResponse * * @return boolean|\CodeIgniter\HTTP\RequestInterface|\CodeIgniter\HTTP\Response|\CodeIgniter\HTTP\ResponseInterface|mixed - * @throws \CodeIgniter\Filters\Exceptions\FilterException + * @throws \CodeIgniter\Router\Exceptions\RedirectException * @throws \Exception */ public function run(RouteCollectionInterface $routes = null, bool $returnResponse = false) @@ -244,7 +244,7 @@ public function run(RouteCollectionInterface $routes = null, bool $returnRespons { return $this->handleRequest($routes, $cacheConfig, $returnResponse); } - catch (FilterException $e) + catch (RedirectException $e) { $logger = Services::logger(); $logger->info('REDIRECTED ROUTE at ' . $e->getMessage()); @@ -252,6 +252,8 @@ public function run(RouteCollectionInterface $routes = null, bool $returnRespons // If the route is a 'redirect' route, it throws // the exception with the $to as the message $this->response->redirect($e->getMessage(), 'auto', $e->getCode()); + $this->sendResponse(); + $this->callExit(EXIT_SUCCESS); } catch (PageNotFoundException $e) diff --git a/system/Router/Exceptions/RedirectException.php b/system/Router/Exceptions/RedirectException.php index c1f68620b46e..5c3f695570cd 100644 --- a/system/Router/Exceptions/RedirectException.php +++ b/system/Router/Exceptions/RedirectException.php @@ -8,8 +8,5 @@ class RedirectException extends \Exception { - public static function forUnableToRedirect(string $route, string $code) - { - return new static(lang('Redirect.forUnableToRedirect', [$route, $code])); - } + protected $code = 302; } diff --git a/system/Router/Router.php b/system/Router/Router.php index 0b79af581690..7f010ca22eaf 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -484,7 +484,7 @@ protected function checkRoutes(string $uri): bool // Is this route supposed to redirect to another? if ($this->collection->isRedirect($key)) { - throw RedirectException::forUnableToRedirect($val, $this->collection->getRedirectCode($key)); + throw new RedirectException($val, $this->collection->getRedirectCode($key)); } $this->setRequest(explode('/', $val)); diff --git a/user_guide_src/source/general/errors.rst b/user_guide_src/source/general/errors.rst index 781bd3083c37..97d2658f7cd3 100644 --- a/user_guide_src/source/general/errors.rst +++ b/user_guide_src/source/general/errors.rst @@ -123,3 +123,16 @@ or when it is temporarily lost:: throw new \CodeIgniter\Database\Exceptions\DatabaseException(); This provides an HTTP status code of 500 and an exit code of 8. + +RedirectException +----------------- + +This exception is a special case allowing for overriding of all other response routing and +forcing a redirect to a specific route or URL. + + throw new \CodeIgniter\Router\Exceptions\RedirectException($route); + +``$route`` may be a named route, relative URI, or a complete URL. You can also supply a +redirect code to use instead of the default (``302``, "temporary redirect"): + + throw new \CodeIgniter\Router\Exceptions\RedirectException($route, 301);