Skip to content

Commit 6f5229a

Browse files
committed
fix: prevent gatherOutput from being called twice when controller returns Response
1 parent 680873b commit 6f5229a

3 files changed

Lines changed: 52 additions & 16 deletions

File tree

‎system/CodeIgniter.php‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -413,26 +413,27 @@ protected function handleRequest(?RouteCollectionInterface $routes, ?Cache $cach
413413
// If startController returned a Response (from an attribute or Closure), use it
414414
if ($returned instanceof ResponseInterface) {
415415
$this->gatherOutput($returned);
416-
}
417-
// Closure controller has run in startController() - benchmarks were
418-
// stopped there as well.
419-
elseif (! is_callable($this->controller)) {
420-
$controller = $this->createController();
416+
} else {
417+
// Closure controller has run in startController() - benchmarks were
418+
// stopped there as well.
419+
if (! is_callable($this->controller)) {
420+
$controller = $this->createController();
421421

422-
if (! method_exists($controller, '_remap') && ! is_callable([$controller, $this->method], false)) {
423-
throw PageNotFoundException::forMethodNotFound($this->method);
424-
}
422+
if (! method_exists($controller, '_remap') && ! is_callable([$controller, $this->method], false)) {
423+
throw PageNotFoundException::forMethodNotFound($this->method);
424+
}
425425

426-
// Is there a "post_controller_constructor" event?
427-
Events::trigger('post_controller_constructor');
426+
// Is there a "post_controller_constructor" event?
427+
Events::trigger('post_controller_constructor');
428428

429-
$returned = $this->runController($controller);
430-
}
429+
$returned = $this->runController($controller);
430+
}
431431

432-
// If $returned is a string, then the controller output something,
433-
// probably a view, instead of echoing it directly. Send it along
434-
// so it can be used with the output.
435-
$this->gatherOutput($returned);
432+
// If $returned is a string, then the controller output something,
433+
// probably a view, instead of echoing it directly. Send it along
434+
// so it can be used with the output.
435+
$this->gatherOutput($returned);
436+
}
436437

437438
if ($this->enableFilters) {
438439
/** @var Filters $filters */

‎tests/system/CodeIgniterTest.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,4 +1330,37 @@ public function testResetForWorkerModeDoesNotLoadCspWhenDisabled(): void
13301330
$this->assertNull(RichRenderer::$css_nonce);
13311331
$this->assertTrue(RichRenderer::$needs_pre_render);
13321332
}
1333+
1334+
public function testGatherOutputCalledOnceWhenControllerReturnsResponse(): void
1335+
{
1336+
$this->resetServices();
1337+
1338+
$superglobals = service('superglobals');
1339+
$superglobals->setServer('argv', ['index.php', 'pages/test']);
1340+
$superglobals->setServer('argc', 2);
1341+
$superglobals->setServer('REQUEST_URI', '/pages/test');
1342+
$superglobals->setServer('SCRIPT_NAME', '/index.php');
1343+
1344+
$routes = service('routes');
1345+
$routes->add('pages/test', static fn () => service('response')->setBody('Test Body'));
1346+
1347+
$config = new App();
1348+
$codeigniter = new class ($config) extends MockCodeIgniter {
1349+
public int $gatherOutputCalls = 0;
1350+
1351+
protected function gatherOutput($returned = null): void
1352+
{
1353+
$this->gatherOutputCalls++;
1354+
parent::gatherOutput($returned);
1355+
}
1356+
};
1357+
1358+
ob_start();
1359+
$codeigniter->run($routes);
1360+
ob_end_clean();
1361+
1362+
// When startController() returns a ResponseInterface (e.g. from a closure route),
1363+
// gatherOutput() must be called exactly once — not twice as in the original bug.
1364+
$this->assertSame(1, $codeigniter->gatherOutputCalls);
1365+
}
13331366
}

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ Deprecations
401401
Bugs Fixed
402402
**********
403403

404+
- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).
405+
404406
See the repo's
405407
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
406408
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)