When I run a controller that has a
$this->request->isCLI()
checks it shows an error when I call this controller from a command line.
The error:
Call to undefined method CodeIgniter\HTTP\CLIRequest::isCLI()
When I call the controller from a browser, it works just as expected.
How to Reproduce the error:
Install a new fresh version of CodeIgniter appstarter via composer.
Change environment to development.
In any controller, Home, for exemple, just write:
if ($this->request->isCLI()) {
echo "Ok";
} else {
echo "Not allowed";
}
Start local server: php spark serve
Now, call this controller from a web browser. It will show "Not allowed" as expected.
Now, call this controller from a command line:
php public/index.php
Describe the bug
isCLI() is not working when it is called from command line
CodeIgniter 4 version
4.04 - Appstarter fresh installation
Context
I think this is related to #3309 with regard to how the Request object is resolved currently. See my comment there.
From the current logic, running php public/index.php under a development environment will be resolved under the second if, resulting to the Request object being an instance of CLIRequest. Since the issue hasn't been resolved yet, we just have to wait and see, perhaps.
I guess we have to add CLIRequest::isCLI() method because otherwise IncomingRequest::isCLI() has not much sense.
As always - PRs are welcome.
Had same issue. Well in my case I used the global function is_cli(). According to the docs it returns TRUE if the script is being executed from the command line.