We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Unknown error when running command-line app.

Hi all,

I'm trying to run a command-line task, and my cli.php file is giving me this error:

PHP Notice:  Array to string conversion in /var/www/htdocs/classschedule/app/cli.php on line 23
PHP Fatal error:  Uncaught RuntimeException: Call to undefined method ::gettaskname() in /var/www/htdocs/classschedule/app/cli.php:23
Stack trace:
#0 /var/www/htdocs/classschedule/app/cli.php(23): Phalcon\Cli\Console->handle(Array)
#1 {main}
  thrown in /var/www/htdocs/classschedule/app/cli.php on line 23

Here is my cli.php

<?php
include '/var/www/common/dump.php';
require 'config/bootstrap.php';

$DI->get('dispatcher')->setDefaultNamespace('Task');
$DI->get('dispatcher')->setNamespaceName('Task');

$Console = new \Phalcon\CLI\Console();
$Console->setDI($DI);

$arguments = [];
foreach($argv as $k => $arg) {
    if($k == 1) {
        $arguments['task'] = $arg;
    } elseif($k == 2) {
         $arguments['action'] = $arg;
    } elseif($k >= 3) {
        $arguments['params'][] = $arg;
    }
}

try{
    $Console->handle($arguments); // <-- This is line 23
}
catch(\Phalcon\Exception $e){
    echo $e->getMessage();
    exit(255);
}

I have no idea why either the Notice or Fatal error are getting generated. This file is almost identical to the cli.php for another app I have, that runs just fine. Even taking out the foreach() still causes the error.

edited Sep '18

Somehow you/framework is trying to call invalid method. public getTaskName ()

The method seems to be correct, but it's object (Router) is missing?

It must be that something went wrong behind your code:

include '/var/www/common/dump.php';
require 'config/bootstrap.php';

$DI->get('dispatcher')->setDefaultNamespace('Task');
$DI->get('dispatcher')->setNamespaceName('Task');

Try w/o setting namespace here.

Have you tried basic example from: https://docs.phalcon.io/en/3.4/application-cli

The bootstrap.php file can be found here: https://pastebin.com/QRthz5Tm. But even if I completely comment out that require() and call $DI = new \Phalcon\DI\FactoryDefault();, I still get the error.



125.8k
Accepted
answer

The problems were legion. My DI, Router, and Dispatcher were all MVC versions, not CLI versions. The setTask() method is in the CLI dispatcher.

@stamster Thanks for the advice to check the basic example. That pointed me in the right direction.