Skip to content

Commit efa0991

Browse files
authored
Merge pull request #7545 from iRedds/move-redirect-exception
[4.4] refactor: moving RedirectException.
2 parents 5ea0f6f + 57af142 commit efa0991

File tree

12 files changed

+65
-11
lines changed

12 files changed

+65
-11
lines changed

system/CodeIgniter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
use CodeIgniter\Exceptions\PageNotFoundException;
1919
use CodeIgniter\HTTP\CLIRequest;
2020
use CodeIgniter\HTTP\DownloadResponse;
21+
use CodeIgniter\HTTP\Exceptions\RedirectException;
2122
use CodeIgniter\HTTP\IncomingRequest;
2223
use CodeIgniter\HTTP\RedirectResponse;
2324
use CodeIgniter\HTTP\Request;
2425
use CodeIgniter\HTTP\ResponseInterface;
2526
use CodeIgniter\HTTP\URI;
26-
use CodeIgniter\Router\Exceptions\RedirectException;
27+
use CodeIgniter\Router\Exceptions\RedirectException as DeprecatedRedirectException;
2728
use CodeIgniter\Router\RouteCollectionInterface;
2829
use CodeIgniter\Router\Router;
2930
use Config\App;
@@ -343,7 +344,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
343344

344345
try {
345346
$this->response = $this->handleRequest($routes, config(Cache::class), $returnResponse);
346-
} catch (RedirectException $e) {
347+
} catch (RedirectException|DeprecatedRedirectException $e) {
347348
$this->outputBufferingEnd();
348349
$logger = Services::logger();
349350
$logger->info('REDIRECTED ROUTE at ' . $e->getMessage());

system/Commands/Utilities/Routes/FilterFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use CodeIgniter\Exceptions\PageNotFoundException;
1515
use CodeIgniter\Filters\Filters;
16-
use CodeIgniter\Router\Exceptions\RedirectException;
16+
use CodeIgniter\HTTP\Exceptions\RedirectException;
1717
use CodeIgniter\Router\Router;
1818
use Config\Services;
1919

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\HTTP\Exceptions;
13+
14+
use CodeIgniter\Exceptions\HTTPExceptionInterface;
15+
use Exception;
16+
17+
/**
18+
* RedirectException
19+
*/
20+
class RedirectException extends Exception implements HTTPExceptionInterface
21+
{
22+
/**
23+
* HTTP status code for redirects
24+
*
25+
* @var int
26+
*/
27+
protected $code = 302;
28+
}

system/Router/Exceptions/RedirectException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
/**
1818
* RedirectException
19+
*
20+
* @deprecated Use \CodeIgniter\HTTP\Exceptions\RedirectException instead
1921
*/
2022
class RedirectException extends Exception implements HTTPExceptionInterface
2123
{

system/Router/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use Closure;
1515
use CodeIgniter\Exceptions\PageNotFoundException;
16+
use CodeIgniter\HTTP\Exceptions\RedirectException;
1617
use CodeIgniter\HTTP\Request;
17-
use CodeIgniter\Router\Exceptions\RedirectException;
1818
use CodeIgniter\Router\Exceptions\RouterException;
1919

2020
/**

system/Test/FeatureTestCase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
namespace CodeIgniter\Test;
1313

1414
use CodeIgniter\Events\Events;
15+
use CodeIgniter\HTTP\Exceptions\RedirectException;
1516
use CodeIgniter\HTTP\IncomingRequest;
1617
use CodeIgniter\HTTP\Request;
1718
use CodeIgniter\HTTP\URI;
1819
use CodeIgniter\HTTP\UserAgent;
19-
use CodeIgniter\Router\Exceptions\RedirectException;
20-
use CodeIgniter\Router\RouteCollection;
2120
use Config\Services;
2221
use Exception;
2322
use ReflectionException;

system/Test/FeatureTestTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
namespace CodeIgniter\Test;
1313

1414
use CodeIgniter\Events\Events;
15+
use CodeIgniter\HTTP\Exceptions\RedirectException;
1516
use CodeIgniter\HTTP\IncomingRequest;
1617
use CodeIgniter\HTTP\Request;
1718
use CodeIgniter\HTTP\URI;
18-
use CodeIgniter\Router\Exceptions\RedirectException;
19-
use CodeIgniter\Router\RouteCollection;
2019
use Config\App;
2120
use Config\Services;
2221
use Exception;

tests/system/CodeIgniterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Config\Services;
1515
use CodeIgniter\Exceptions\ConfigException;
1616
use CodeIgniter\HTTP\Response;
17+
use CodeIgniter\Router\Exceptions\RedirectException;
1718
use CodeIgniter\Router\RouteCollection;
1819
use CodeIgniter\Test\CIUnitTestCase;
1920
use CodeIgniter\Test\Filters\CITestStreamFilter;
@@ -570,6 +571,29 @@ public function testRunRedirectionWithPOSTAndHTTPCode301()
570571
$this->assertSame(301, $response->getStatusCode());
571572
}
572573

574+
/**
575+
* test for deprecated \CodeIgniter\Router\Exceptions\RedirectException for backward compatibility
576+
*/
577+
public function testRedirectExceptionDeprecated(): void
578+
{
579+
$_SERVER['argv'] = ['index.php', '/'];
580+
$_SERVER['argc'] = 2;
581+
582+
// Inject mock router.
583+
$routes = Services::routes();
584+
$routes->get('/', static function () {
585+
throw new RedirectException('redirect-exception', 503);
586+
});
587+
588+
$router = Services::router($routes, Services::incomingrequest());
589+
Services::injectMock('router', $router);
590+
591+
$response = $this->codeigniter->run($routes, true);
592+
593+
$this->assertSame(503, $response->getStatusCode());
594+
$this->assertSame('http://example.com/redirect-exception', $response->getHeaderLine('Location'));
595+
}
596+
573597
public function testStoresPreviousURL()
574598
{
575599
$_SERVER['argv'] = ['index.php', '/'];

tests/system/Router/RouterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use CodeIgniter\Config\Services;
1515
use CodeIgniter\Exceptions\PageNotFoundException;
16+
use CodeIgniter\HTTP\Exceptions\RedirectException;
1617
use CodeIgniter\HTTP\IncomingRequest;
17-
use CodeIgniter\Router\Exceptions\RedirectException;
1818
use CodeIgniter\Router\Exceptions\RouterException;
1919
use CodeIgniter\Test\CIUnitTestCase;
2020
use Config\Modules;

user_guide_src/source/changelogs/v4.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Deprecations
146146
``ExceptionHandler``.
147147
- **Autoloader:** ``Autoloader::sanitizeFilename()`` is deprecated.
148148
- **CodeIgniter:** ``CodeIgniter::$returnResponse`` property is deprecated. No longer used.
149+
- **RedirectException:** ``\CodeIgniter\Router\Exceptions\RedirectException`` is deprecated. Use \CodeIgniter\HTTP\Exceptions\RedirectException instead.
149150

150151
Bugs Fixed
151152
**********
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
throw new \CodeIgniter\Router\Exceptions\RedirectException($route);
3+
throw new \CodeIgniter\HTTP\Exceptions\RedirectException($route);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
throw new \CodeIgniter\Router\Exceptions\RedirectException($route, 301);
3+
throw new \CodeIgniter\HTTP\Exceptions\RedirectException($route, 301);

0 commit comments

Comments
 (0)