Skip to content

Commit

Permalink
Merge pull request #2338 from MGatner/redirectexception
Browse files Browse the repository at this point in the history
Revert RedirectException change
  • Loading branch information
MGatner authored Oct 16, 2019
2 parents 65cc1f4 + cc638d8 commit 4b5d922
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
8 changes: 5 additions & 3 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -244,14 +244,16 @@ 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());

// 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)
Expand Down
5 changes: 1 addition & 4 deletions system/Router/Exceptions/RedirectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
13 changes: 13 additions & 0 deletions user_guide_src/source/general/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 4b5d922

Please sign in to comment.