CodeIgniter Forums
CLI access missing variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: CLI access missing variables (/showthread.php?tid=68062)



CLI access missing variables - Sam - 05-18-2017

Hello to the CI community!

I have a fundamental question about the whether running via the CLI has any inherent limitations, versus calling via a standard HTTP request.

Specifically, I have a CI app that includes a custom controller designed to be called at regular intervals via a cron job. This controller carries out a variety of tasks, including querying the database and sending out emails. It extends my custom MY_Controller and leverages multiple CI helper functions and libraries. In testing I've found that I can call this controller successfully via the CLI like so:
Code:
*/5 * * * * cd /path/to/app/index; php index.php Cron_controller cron_method >/dev/null 2>&1
, following the example laid out in the user guide. For the most part this works, but it seems that my Cron_controller doesn't have access to all of the CI functions and variables that I would expect. Base_url() doesn't return anything and template variables that are expected by my view files aren't found, to name a few.

If I call this controller directly with an HTTP request, base_url() and other helper functions and variables work as expected. It's only when I call this via the CLI that the expected values don't come through.

Why does my controller behave differently when invoked via the CLI?


RE: CLI access missing variables - kilishan - 05-18-2017

This is not a limitation of CodeIgniter, but a limitation of PHP and your web server. On the CLI certain things are simply not available. There's no HTTP request so anything to do with that will be non-functional, including some $_SERVER settings that would come from that. Sessions don't work.


RE: CLI access missing variables - Sam - 05-19-2017

(05-18-2017, 07:36 PM)kilishan Wrote: This is not a limitation of CodeIgniter, but a limitation of PHP and your web server. On the CLI certain things are simply not available. There's no HTTP request so anything to do with that will be non-functional, including some $_SERVER settings that would come from that. Sessions don't work.

That makes sense, thanks. Is there something about the base_url() function that relies on these $_SERVER settings being present? I thought hard-coding the $config['base_url'] value in my config.php would do the trick, but my links remain broken despite this.

As a fallback I am able to call my script using curl, but I would prefer the CLI method over curl if I can get it working.


RE: CLI access missing variables - rtenny - 05-26-2017

there is a global function called is_cli() which you can use to pre-set things you need when in CLI mode

if (is_cli()) {}

There is also different configs being loaded depending on the ENVIRONMENT maybe that why you base_url is not set.