CodeIgniter Forums
CLI Request Object not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CLI Request Object not working (/showthread.php?tid=86891)



CLI Request Object not working - MrBungle - 02-23-2023

Hello all,
I am struggling for a long time now with the CodeIgniter CLI classes.
Unfortunately, the docs are anything but verbose there.
What I need is call a controller from the command line with optional named, optional parameters.

My route is

PHP Code:
$routes->cli('cron/makeactiontables''CLI\MakeActionTables::createActions'); 

And I call this route on the command line like this

Code:
php index.php cron makeactiontables --id=32654

My Controller looks like this


PHP Code:
<?php
namespace App\Controllers\CLI;

use 
CodeIgniter\CLI\CLI;
use 
CodeIgniter\Controller;

class 
MakeActionTables extends Controller {

  public function __construct() {
  }

  public function createActions() {
    print_r($this->request->getOptions());
    return true
The return is

Code:
    [Error]

    Call to undefined method CodeIgniter\HTTP\IncomingRequest::getOptions()

The same happens when I extend from BaseController instead of Controller.
The docs say


Quote:If a request comes from a command line invocation, the request object is actually a CLIRequest. It behaves the same as a conventional request but adds some accessor methods for convenience.

And it lists all these beautiful methods that I need but it does not seem to work.

And yes, I know, the documentation also says

Quote:It is recommended to use Spark Commands for CLI scripts instead of calling controllers via CLI

However, I can't do that, really I can't, plus the above mentioned functions are there for a reason I thought Smile

Any idea what I am doing wrong?
Or where I find a more in depth documentation and/or examples for this CLI stuff?


RE: CLI Request Object not working - kenjis - 02-23-2023

What version of CI4 do you use?

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        print_r($this->request->getOptions());
    }


PHP Code:
$routes->cli('cron/makeactiontables''Home::index'); 

Code:
$ php public/index.php cron makeactiontables --id=32654
Array
(
    [id=32654] =>
)



RE: CLI Request Object not working - MrBungle - 02-24-2023

I am using V 4.2.1

... you set me on the right path there... I upgraded to 4.3 and it works so far.
Thanks!! Smile


RE: CLI Request Object not working - kenjis - 02-24-2023

It was a bug in v4.2.1, and fixed in v4.2.2.

Yes, using the latest version is good practice.

Don't forget to check Upgrading Guide:
https://codeigniter4.github.io/CodeIgniter4/installation/upgrading.html
There might be breaking changes that break your app.


RE: CLI Request Object not working - MrBungle - 02-24-2023

kenjisIt was a bug in v4.2.1, and fixed in v4.2.2.

Yes, using the latest version is good practice.

Don't forget to check Upgrading Guide:
https://codeigniter4.github.io/CodeIgniter4/installation/upgrading.html
There might be breaking changes that break your app.

Indeed there were a few, but nothing dramatic.
Thanks again!