@@ -159,6 +159,11 @@ class CodeIgniter
159159 */
160160 protected bool $ enableFilters = true ;
161161
162+ /**
163+ * Whether to return Response object or send response.
164+ */
165+ protected bool $ returnResponse = false ;
166+
162167 /**
163168 * Constructor.
164169 */
@@ -312,6 +317,8 @@ private function configureKint(): void
312317 */
313318 public function run (?RouteCollectionInterface $ routes = null , bool $ returnResponse = false )
314319 {
320+ $ this ->returnResponse = $ returnResponse ;
321+
315322 if ($ this ->context === null ) {
316323 throw new LogicException (
317324 'Context must be set before run() is called. If you are upgrading from 4.1.x, '
@@ -333,6 +340,10 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
333340 if ($ this ->request instanceof IncomingRequest && strtolower ($ this ->request ->getMethod ()) === 'cli ' ) {
334341 $ this ->response ->setStatusCode (405 )->setBody ('Method Not Allowed ' );
335342
343+ if ($ this ->returnResponse ) {
344+ return $ this ->response ;
345+ }
346+
336347 $ this ->sendResponse ();
337348
338349 return ;
@@ -364,13 +375,22 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
364375 // If the route is a 'redirect' route, it throws
365376 // the exception with the $to as the message
366377 $ this ->response ->redirect (base_url ($ e ->getMessage ()), 'auto ' , $ e ->getCode ());
378+
379+ if ($ this ->returnResponse ) {
380+ return $ this ->response ;
381+ }
382+
367383 $ this ->sendResponse ();
368384
369385 $ this ->callExit (EXIT_SUCCESS );
370386
371387 return ;
372388 } catch (PageNotFoundException $ e ) {
373- $ this ->display404errors ($ e );
389+ $ return = $ this ->display404errors ($ e );
390+
391+ if ($ return instanceof ResponseInterface) {
392+ return $ return ;
393+ }
374394 }
375395 }
376396
@@ -421,9 +441,13 @@ public function disableFilters(): void
421441 *
422442 * @throws PageNotFoundException
423443 * @throws RedirectException
444+ *
445+ * @deprecated $returnResponse is deprecated.
424446 */
425447 protected function handleRequest (?RouteCollectionInterface $ routes , Cache $ cacheConfig , bool $ returnResponse = false )
426448 {
449+ $ this ->returnResponse = $ returnResponse ;
450+
427451 $ routeFilter = $ this ->tryToRouteIt ($ routes );
428452
429453 $ uri = $ this ->determinePath ();
@@ -453,7 +477,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
453477
454478 // If a ResponseInterface instance is returned then send it back to the client and stop
455479 if ($ possibleResponse instanceof ResponseInterface) {
456- return $ returnResponse ? $ possibleResponse : $ possibleResponse ->send ();
480+ return $ this -> returnResponse ? $ possibleResponse : $ possibleResponse ->send ();
457481 }
458482
459483 if ($ possibleResponse instanceof Request) {
@@ -528,7 +552,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
528552
529553 unset($ uri );
530554
531- if (! $ returnResponse ) {
555+ if (! $ this -> returnResponse ) {
532556 $ this ->sendResponse ();
533557 }
534558
@@ -919,6 +943,8 @@ protected function runController($class)
919943 /**
920944 * Displays a 404 Page Not Found error. If set, will try to
921945 * call the 404Override controller/method that was set in routing config.
946+ *
947+ * @return ResponseInterface|void
922948 */
923949 protected function display404errors (PageNotFoundException $ e )
924950 {
@@ -943,6 +969,10 @@ protected function display404errors(PageNotFoundException $e)
943969
944970 $ cacheConfig = new Cache ();
945971 $ this ->gatherOutput ($ cacheConfig , $ returned );
972+ if ($ this ->returnResponse ) {
973+ return $ this ->response ;
974+ }
975+
946976 $ this ->sendResponse ();
947977
948978 return ;
0 commit comments