Skip to content

Commit

Permalink
fix: Fix the filters are executed even when controller does not exist…
Browse files Browse the repository at this point in the history
… with Auto Routing (Legacy).
  • Loading branch information
ping-yee committed Sep 11, 2023
1 parent eb1d5e0 commit 40c098c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
20 changes: 19 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache

$uri = $this->determinePath();

if ($this->enableFilters) {
if ($this->enableFilters && $this->checkControllerNotFoundBeforeFilter()) {
// Start up the filters
$filters = Services::filters();

Expand Down Expand Up @@ -1131,4 +1131,22 @@ protected function outputBufferingEnd(): string

return $buffer;
}

/**
* Check whether the controller is not found before the before filter.
*/
protected function checkControllerNotFoundBeforeFilter(): bool
{
// Check whether the controller type is Closure.
if (is_object($this->controller) && (get_class($this->controller) === 'Closure')) {
return true;
}

// Try to autoload the class
if (! class_exists($this->controller, true) || $this->method[0] === '_') {
throw PageNotFoundException::forControllerNotFound($this->controller, $this->method);
}

return true;
}
}
21 changes: 21 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\RouteCollection;
Expand Down Expand Up @@ -924,4 +925,24 @@ public static function providePageCacheWithCacheQueryString(): iterable
'$cacheQueryString=array' => [['important_parameter'], 3, $testingUrls],
];
}

/**
* See https://github.com/codeigniter4/CodeIgniter4/issues/7205
*/
public function testRunControllerNotFoundBeforeFilter(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;

$_SERVER['REQUEST_URI'] = '/cannotFound';
$_SERVER['SCRIPT_NAME'] = '/index.php';

// Inject mock router.
$routes = Services::routes();
$routes->setAutoRoute(true);

$this->expectException(PageNotFoundException::class);

$this->codeigniter->run($routes);
}
}

0 comments on commit 40c098c

Please sign in to comment.