Codeigniter4: Bug: isCLI error when calling a controller from a Command Line

Created on 17 Jul 2020  路  3Comments  路  Source: codeigniter4/CodeIgniter4

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

  • It wil trhow the error: Call to undefined method CodeIgniter\HTTP\CLIRequest::isCLI()

Describe the bug
isCLI() is not working when it is called from command line

CodeIgniter 4 version
4.04 - Appstarter fresh installation

Context

  • OS: Windows 10, localmachine
  • PHP 7.3.11
bug

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bagimuka picture bagimuka  路  5Comments

jim-parry picture jim-parry  路  4Comments

lonnieezell picture lonnieezell  路  7Comments

petk picture petk  路  4Comments

Jack-Dane picture Jack-Dane  路  4Comments