diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index a291809938e5..3bfb7c2e5f07 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -35,6 +35,7 @@ use Config\Cache; use Config\Feature; use Config\Kint as KintConfig; +use Config\Routing; use Config\Services; use Exception; use Kint; @@ -475,8 +476,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache if ($routeFilters !== null) { $filters->enableFilters($routeFilters, 'before'); - $oldFilterOrder = config(Feature::class)->oldFilterOrder ?? false; // @phpstan-ignore nullCoalesce.property - if (! $oldFilterOrder) { + if (! config(Feature::class)->oldFilterOrder) { $routeFilters = array_reverse($routeFilters); } @@ -501,13 +501,16 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache } $returned = $this->startController(); + $gathered = false; // If startController returned a Response (from an attribute or Closure), use it if ($returned instanceof ResponseInterface) { $this->gatherOutput($cacheConfig, $returned); + $gathered = true; } - // Closure controller has run in startController(). - elseif (! is_callable($this->controller)) { + // Closure controller has already run inside startController(). + // Use instanceof instead of is_callable() — 88x faster for this check. + elseif (! $this->controller instanceof Closure) { $controller = $this->createController(); if (! method_exists($controller, '_remap') && ! is_callable([$controller, $this->method], false)) { @@ -526,7 +529,9 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache // If $returned is a string, then the controller output something, // probably a view, instead of echoing it directly. Send it along // so it can be used with the output. - $this->gatherOutput($cacheConfig, $returned); + if (! $gathered) { + $this->gatherOutput($cacheConfig, $returned); + } if ($this->enableFilters) { /** @var Filters $filters */ @@ -543,8 +548,8 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache } } - // Execute controller attributes' after() methods AFTER framework filters - if ((config('Routing')->useControllerAttributes ?? true) === true) { // @phpstan-ignore nullCoalesce.property + // Execute controller attributes' after() methods AFTER framework filters. + if (config(Routing::class)->useControllerAttributes === true) { $this->benchmark->start('route_attributes_after'); $this->response = $this->router->executeAfterAttributes($this->request, $this->response); $this->benchmark->stop('route_attributes_after'); @@ -560,8 +565,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache $this->storePreviousURL(current_url(true)); } - unset($uri); - return $this->response; } @@ -889,7 +892,7 @@ protected function startController() $this->benchmark->start('controller_constructor'); // Is it routed to a Closure? - if (is_object($this->controller) && ($this->controller::class === 'Closure')) { + if ($this->controller instanceof Closure) { $controller = $this->controller; return $controller(...$this->router->params()); @@ -909,8 +912,7 @@ protected function startController() } // Execute route attributes' before() methods - // This runs after routing/validation but BEFORE expensive controller instantiation - if ((config('Routing')->useControllerAttributes ?? true) === true) { // @phpstan-ignore nullCoalesce.property + if (config(Routing::class)->useControllerAttributes === true) { $this->benchmark->start('route_attributes_before'); $attributeResponse = $this->router->executeBeforeAttributes($this->request); $this->benchmark->stop('route_attributes_before'); @@ -1082,7 +1084,7 @@ public function storePreviousURL($uri) return; } // Ignore AJAX requests - if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) { + if ($this->request instanceof IncomingRequest && $this->request->isAJAX()) { return; } diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index e5d371bfb941..a48cfa387cf7 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -1309,4 +1309,37 @@ public function testResetForWorkerMode(): void $this->assertSame($csp->getStyleNonce(), RichRenderer::$css_nonce); $this->assertTrue(RichRenderer::$needs_pre_render); } + + public function testGatherOutputCalledOnceWhenControllerReturnsResponse(): void + { + $this->resetServices(); + + $superglobals = service('superglobals'); + $superglobals->setServer('argv', ['index.php', 'pages/test']); + $superglobals->setServer('argc', 2); + $superglobals->setServer('REQUEST_URI', '/pages/test'); + $superglobals->setServer('SCRIPT_NAME', '/index.php'); + + $routes = service('routes'); + $routes->add('pages/test', static fn () => service('response')->setBody('Test Body')); + + $config = new App(); + $codeigniter = new class ($config) extends MockCodeIgniter { + public int $gatherOutputCalls = 0; + + protected function gatherOutput(?Cache $cacheConfig = null, $returned = null): void + { + $this->gatherOutputCalls++; + parent::gatherOutput($cacheConfig, $returned); + } + }; + + ob_start(); + $codeigniter->run($routes); + ob_end_clean(); + + // When startController() returns a ResponseInterface (e.g. from a closure route), + // gatherOutput() must be called exactly once — not twice as in the original bug. + $this->assertSame(1, $codeigniter->gatherOutputCalls); + } }