Skip to content

Commit ec97332

Browse files
committed
fix: CodeIgniter::run() doesn't respect $returnResponse parameter
1 parent 6d40429 commit ec97332

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

system/CodeIgniter.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ class CodeIgniter
151151
*/
152152
protected ?string $context = null;
153153

154+
/**
155+
* Whether to return Response object or send response.
156+
*/
157+
protected bool $returnResponse = false;
158+
154159
/**
155160
* Constructor.
156161
*/
@@ -291,6 +296,8 @@ protected function initializeKint()
291296
*/
292297
public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false)
293298
{
299+
$this->returnResponse = $returnResponse;
300+
294301
if ($this->context === null) {
295302
throw new LogicException('Context must be set before run() is called. If you are upgrading from 4.1.x, you need to merge `public/index.php` and `spark` file from `vendor/codeigniter4/framework`.');
296303
}
@@ -309,7 +316,11 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
309316
if ($this->request instanceof IncomingRequest && strtolower($this->request->getMethod()) === 'cli') {
310317
$this->response->setStatusCode(405)->setBody('Method Not Allowed');
311318

312-
$this->sendResponse();
319+
if (! $this->returnResponse) {
320+
$this->sendResponse();
321+
} else {
322+
return $this->response;
323+
}
313324

314325
return;
315326
}
@@ -345,13 +356,22 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
345356
// If the route is a 'redirect' route, it throws
346357
// the exception with the $to as the message
347358
$this->response->redirect(base_url($e->getMessage()), 'auto', $e->getCode());
348-
$this->sendResponse();
359+
360+
if (! $this->returnResponse) {
361+
$this->sendResponse();
362+
} else {
363+
return $this->response;
364+
}
349365

350366
$this->callExit(EXIT_SUCCESS);
351367

352368
return;
353369
} catch (PageNotFoundException $e) {
354-
$this->display404errors($e);
370+
$return = $this->display404errors($e);
371+
372+
if ($return instanceof ResponseInterface) {
373+
return $return;
374+
}
355375
}
356376
}
357377

@@ -400,6 +420,8 @@ private function isWeb(): bool
400420
*
401421
* @throws PageNotFoundException
402422
* @throws RedirectException
423+
*
424+
* @deprecated $returnResponse is deprecated, and no longer used.
403425
*/
404426
protected function handleRequest(?RouteCollectionInterface $routes, Cache $cacheConfig, bool $returnResponse = false)
405427
{
@@ -433,7 +455,8 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
433455

434456
// If a ResponseInterface instance is returned then send it back to the client and stop
435457
if ($possibleResponse instanceof ResponseInterface) {
436-
return $returnResponse ? $possibleResponse : $possibleResponse->pretend($this->useSafeOutput)->send();
458+
return $this->returnResponse ? $possibleResponse
459+
: $possibleResponse->pretend($this->useSafeOutput)->send();
437460
}
438461

439462
if ($possibleResponse instanceof Request) {
@@ -512,7 +535,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
512535

513536
unset($uri);
514537

515-
if (! $returnResponse) {
538+
if (! $this->returnResponse) {
516539
$this->sendResponse();
517540
}
518541

@@ -910,6 +933,8 @@ protected function runController($class)
910933
/**
911934
* Displays a 404 Page Not Found error. If set, will try to
912935
* call the 404Override controller/method that was set in routing config.
936+
*
937+
* @return ResponseInterface|void
913938
*/
914939
protected function display404errors(PageNotFoundException $e)
915940
{
@@ -934,7 +959,11 @@ protected function display404errors(PageNotFoundException $e)
934959

935960
$cacheConfig = new Cache();
936961
$this->gatherOutput($cacheConfig, $returned);
937-
$this->sendResponse();
962+
if (! $this->returnResponse) {
963+
$this->sendResponse();
964+
} else {
965+
return $this->response;
966+
}
938967

939968
return;
940969
}

0 commit comments

Comments
 (0)