Skip to content

Bug: sendResponse doesn't respect returnResponse parameter in codeigniter.php #6650

Closed
@AmitSonkhiya

Description

@AmitSonkhiya

PHP Version

8.1

CodeIgniter4 Version

4.2.7

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

cli

Database

MariaDB 10.2

What happened?

File in the question: system/CodeIgniter.php

The run() method (line: 292) in the file has the second parameter to return the response instead of printing it.
However, in the case of page not found exception, the script straight prints the result.
Which makes it harder to cache the exception response in the caller script.

Indeed there are other ways to deal with the issue but all of them require extending a few methods in the child class (extending Codeigniter.php) just to include a return statement.

Steps to Reproduce

  1. Use a non-traditional server like ReactPHP or Swoole.
  2. Initiate requests from the server to CodeIgniter.
$app = Services::codeigniter(config('App'));
$app->initialize();
$app->setContext('php-cli');
  1. Execute the run() method along with $returnResponse set to true.
    $app->run(null, true);
  2. Override default 404 page with a controller/method.
  3. Request a resource that doesn't exist.
  4. In the case of a response code 200, CI will return the HTTP\Response object.
  5. In the case of a response code 404, CI will return null and the result (HTML formatted response) will be written to the server's log file.

I haven't tested but the behavior might be seen in the case of response code 500 as well.

Expected Output

When there is a Page not found exception, the server should return the response object instead of printing (echoing) it.
Same is true when a internal server error occurs.

Anything else?

I have extended the core class to overcome this issue but I strongly believe that the framework should respect the type of return requested for either of the HTTP response codes.

Hence it, would be better to make $returnResponse as a class property and change the sendResponse() method's behavior.

Otherwise below are lines to update in the system/CodeIgniter.php file to let the the application return the response when it is asked rather than echoing when a page not found or server error exceptions occur.

Line 312 in the run() method
return $this->sendResponse($returnResponse);

Line 348 in the run() method
return $this->sendResponse($returnResponse);

Line 354 pass the parameter
$this->display404errors($e, $returnResponse);

Line 914 method signature
protected function display404errors(PageNotFoundException $e, bool $returnResponse = false)

Line 937 in the display404errors() method
return $this->sendResponse($returnResponse);

Line 1072 sendResponse() method

    protected function sendResponse($returnResponse = false)
    {
        if (!$returnResponse) {
			$this->response->pretend($this->useSafeOutput)->send();
			return;
		}
		return $this->response;
    }

Metadata

Metadata

Assignees

Labels

bugVerified issues on the current code behavior or pull requests that will fix them

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions