For one of our products, we still have a Codeigniter 3 instance running.  Whilst we’re upgrading it to Laravel we still need to introduce new features and manage performance.  One element of this is scheduled tasks which we currently handle using php-cron-scheduler which does a great job.  Sadly Codeigniter doesn’t always do such a great job so we had to resort to using https:// calls via CURL to the app to trigger syncronisations. As the app is multi-tenanted the url is of course important to distinguish the customer whose data needs synchronising and the CLI was going to cause us issues here…

After lots of messing around, and not wanting to add an unnecessary variable to EVERY synchronisation call AND move the tenant identification away from the MY_Controller.php file where it’s been working happily for the past 2 years, I found my answer in the built in Codeigniter 3 URI Class.

Typically Codeigniter takes the URI segements from your request and through routes you can provide the variables to the methods in your controllers.  Utilising the rsegment_array() method I’m left with an array of ALL of the arguments passed to the CLI so where before we had a GET request to https://tenant.myappontheweb.com/users/syncronise I can now use php public/index.php UserController syncronise tenant.myappontheweb.com to get the same effect with the only requirement to be to add an if(is_cli()) statement to the MY_Controller to differentiate between the https requests and the CLI requests.