Welcome Guest, Not a member yet? Register   Sign In
CLI Request Object not working
#1

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?
Reply
#2

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] =>
)
Reply
#3

(This post was last modified: 02-24-2023, 02:32 AM by MrBungle.)

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
Reply
#4

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/CodeIgnit...ading.html
There might be breaking changes that break your app.
Reply
#5

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/CodeIgnit...ading.html
There might be breaking changes that break your app.

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




Theme © iAndrew 2016 - Forum software by © MyBB