Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,26 +413,27 @@ protected function handleRequest(?RouteCollectionInterface $routes, ?Cache $cach
// If startController returned a Response (from an attribute or Closure), use it
if ($returned instanceof ResponseInterface) {
$this->gatherOutput($returned);
}
// Closure controller has run in startController() - benchmarks were
// stopped there as well.
elseif (! is_callable($this->controller)) {
$controller = $this->createController();
} else {
// Closure controller has run in startController() - benchmarks were
// stopped there as well.
if (! is_callable($this->controller)) {
$controller = $this->createController();

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

// Is there a "post_controller_constructor" event?
Events::trigger('post_controller_constructor');
// Is there a "post_controller_constructor" event?
Events::trigger('post_controller_constructor');

$returned = $this->runController($controller);
}
$returned = $this->runController($controller);
}

// 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($returned);
// 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($returned);
}

if ($this->enableFilters) {
/** @var Filters $filters */
Expand Down
33 changes: 33 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1330,4 +1330,37 @@ public function testResetForWorkerModeDoesNotLoadCspWhenDisabled(): void
$this->assertNull(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($returned = null): void
{
$this->gatherOutputCalls++;
parent::gatherOutput($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);
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ Deprecations
Bugs Fixed
**********

- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.