I test Phalcon CLI on homestead, cli.php:
<?php
use Phalcon\Loader;
use Phalcon\CLI\Console as ConsoleApp;
use Phalcon\DI\FactoryDefault\CLI as CliDI;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
$loader = new Loader();
$loader->registerNamespaces([
'Task' => realpath(__DIR__."/Task/"),
])->registerDirs([
realpath(__DIR__."/Task/"),
])->register();
$arguments = [];
foreach ($argv as $key => $arg) {
if ($key == 1) {
$arguments['task'] = $arg;
} elseif ($key == 2) {
$arguments['action'] = $arg;
} elseif ($key >= 3) {
$arguments['params'][] = $arg;
}
}
try {
$di = new CliDI();
$di->set("db", function (){
$connection = new DbAdapter([
"host" => "localhost",
"username" => "homestead",
"password" => "secret",
"dbname" => "phalcon",
"charset" => "utf8"
]);
return $connection;
});
$console = new ConsoleApp();
$console->setDI($di);
$console->handle($arguments);
} catch(Exception $e) {
$message = get_class($e).": ".$e->getMessage()."\n";
$message .= " File=".$e->getFile()."\n";
$message .= " Line=".$e->getLine()."\n";
$message .= $e->getTraceAsString();
echo $message;
}
And the same directory , Task/MainTask.php:
<?php
namespace Task;
use Phalcon\CLI\Task as BaseTask;
class MainTask extends BaseTask
{
public function mainAction(){
echo "\nThat is the default task and the default action \n";
}
public function testAction(array $params) {
echo sprintf('hello %s', $params[0]) . PHP_EOL;
echo sprintf('best regards, %s', $params[1]) . PHP_EOL;
}
}
When I run this command:
$ php cli.php Main main
But it shows me:
Phalcon\Cli\Dispatcher\Exception: MainTask handler class cannot be loaded
I don't know what's wrong, can you help me?