We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

CLI args

I'm trying to get the controller/method/args passed through to a command line script. In the following discussion it mentions a change in 1.1.0 which means you need to do something different. It doesn't, however, describe what it is you need to do. Can anyone help shed some light on what is needed to get the example code to work?


Many thanks, George


Yes, I saw that, I'm just not sure how to apply that to the following:

$app = new \Phalcon\CLI\Console(); $app->setDI($di); $app->handle($_SERVER['argv']);

In other words, I can set the handle manually as per the unit tests, but I can't seem to work out how to get it to read in commands from the command line.

You need explicitly set what parameter is the task/module/action, etc:

   'task' => $_SERVER['argv'][0]
   'action' => $_SERVER['argv'][1]


Doesn't seems like a better solution, what if the user doesn't give all arguments? How do you handle the case eg:

'module' => 'devtools',
'task' => 'main',
'action' => 'hello',

Where the action takes potentially multiple input.

Is there some kind of parser?

This works for me atm. All tasks in one directory and without modules:

$loader = new \Phalcon\Loader();

        __DIR__ .'/tasks/'

//Using the CLI factory default services container
$di = new \Phalcon\DI\FactoryDefault\CLI();

//Create a console application
$console = new \Phalcon\CLI\Console();

$args = array();
$argv = $_SERVER['argv'];

$args['task'] = isset($argv[1]) ? $argv[1] : 'main';
$args['action'] = isset($argv[2]) ? $argv[2] : 'main';

unset($argv[0], $argv[1], $argv[2]);

$args['params'] = count($argv) > 0 ? array_values($argv) : array();


Maybe this helps: https://forum.phalcon.io/discussion/372/phalcon-cli-apps-with-namespaces-in-phalcon-loader#C1606

But I used it myself also a bit different. I just registered different directories with unique task names:

            $loader = new Phalcon\Loader();
                // FIXME: Loader needs to runs with namespaces