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

\Phalcon\CLI\Router does not work?

Hi. I had a problem with creating CLI app. As I know now we need to pass array with parameters to Phalcon\CLI\Console::handle. So is there a reason to keep Phalcon\CLI\Router? For example:

<?php

$router = new \Phalcon\CLI\Router();

$router->handle($argv);
var_dump($router->getModuleName());
var_dump($router->getTaskName());
var_dump($router->getActionName());
var_dump($router->getParams());

When I run cli command:

php cli.php param1 param2 param3 param4

I get:

NULL
NULL
NULL
array(5) {
  [0]=>
  string(7) "cli.php"
  [1]=>
  string(6) "param1"
  [2]=>
  string(6) "param2"
  [3]=>
  string(6) "param3"
  [4]=>
  string(6) "param4"
}

So router does not work (as I expected). But when I try to create Phalcon\CLI\Console with default DI (Phalcon\DI) exception occurs that I didn't set default router service. It depends on router, but router does not work properly.

It is great that we have flexibility to implement own cli argument parsers and using different cli syntax:

php cli.php taskname actionname param1
or
php cli.php task:sometask action:someaction param2
or
php.cli --action=anotheraction

But currently its now working or am I missing something :)



1.0k

I tried to pass another array to Phalcon\CLI\Router::handle (with task, action keys). After that getTaskName method returned correct task name. But it really does not helped. Example:

<?php

// load dirs e.t.c.

$di = new \Phalcon\DI\FactoryDefault\CLI();

$di->setShared('router', function()
{
    $router = new \Phalcon\CLI\Router();
    $router->handle(array(
        'task' => 'another'
    ));
    return $router;
});

$console = new \Phalcon\CLI\Console($di);
$console->handle();

How can redefine task or action name in router service?



98.9k

Pass those parameters to the method handle of Phalcon\CLI\Console:

$console = new \Phalcon\CLI\Console($di);
$console->handle(array(
        'task' => 'another'
));