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
19 changes: 9 additions & 10 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,13 @@ 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);
}
// Closure controller has run in startController().
elseif (! is_callable($this->controller)) {
$gathered = true;
} elseif (! $this->controller instanceof Closure) {
$controller = $this->createController();

if (! method_exists($controller, '_remap') && ! is_callable([$controller, $this->method], false)) {
Expand All @@ -526,7 +526,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 */
Expand All @@ -543,7 +545,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
}
}

// Execute controller attributes' after() methods AFTER framework filters
// Execute controller attributes' after() methods AFTER framework filters.
if ((config('Routing')->useControllerAttributes ?? true) === true) { // @phpstan-ignore nullCoalesce.property
$this->benchmark->start('route_attributes_after');
$this->response = $this->router->executeAfterAttributes($this->request, $this->response);
Expand All @@ -560,8 +562,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
$this->storePreviousURL(current_url(true));
}

unset($uri);

return $this->response;
}

Expand Down Expand Up @@ -889,7 +889,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());
Expand All @@ -909,7 +909,6 @@ 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
$this->benchmark->start('route_attributes_before');
$attributeResponse = $this->router->executeBeforeAttributes($this->request);
Expand Down Expand Up @@ -1082,7 +1081,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()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This narrows the previous behavior from "any request with isAJAX()" to only IncomingRequest.

I think I'm okay with this, but I would like to hear what others think.

If this stays, please remove the added comment, as it's not relevant to include.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm okay with this too but should at least have a changelog entry as a potential BC break.

return;
}

Expand Down
33 changes: 33 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions user_guide_src/source/changelogs/v4.7.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Release Date: Unreleased
BREAKING
********

- **CodeIgniter:** ``storePreviousURL()`` now checks ``$this->request instanceof IncomingRequest`` instead of using ``method_exists($this->request, 'isAJAX')``. Previously, any request object with an ``isAJAX()`` method was accepted; now only ``IncomingRequest`` instances are checked. If you use a custom request class that implements ``isAJAX()``, ensure it extends ``IncomingRequest``.

***************
Message Changes
***************
Expand All @@ -22,6 +24,9 @@ Message Changes
Changes
*******

- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).
- **CodeIgniter:** Changed ``is_object($this->controller) && $this->controller::class === 'Closure'`` to ``$this->controller instanceof Closure`` for consistency and type safety.

************
Deprecations
************
Expand Down
Loading