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\Dispatcher\Exception: MainTask handler class cannot be loaded

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?



85.5k

loader problem, check what you define ( make sure path is perfectly correct ) .

my code


$loader = new \Phalcon\Loader();
$loader->registerDirs(
    array(
        CLI_PATH . '/tasks',
        CLI_PATH . '/Libs'
    )
);
$loader->register();

Thanks a lot. However, my code directory just like that

[email protected]:~/Code/phalcon-cli$ pwd
/home/vagrant/Code/phalcon-cli
[email protected]:~/Code/phalcon-cli$ ll
total 14
d./xrwxrwx 1 vagrant vagrant 4096 Dec 28 09:08
d../rwxrwx 1 vagrant vagrant 8192 Dec 28 09:07
-rwxrwxrwx 1 vagrant vagrant 1367 Dec 28 09:08 cli.php*
drwxrwxrwx 1 vagrant vagrant    0 Dec 28 02:58 Task/

When I print $loaders:

class Phalcon\Loader#2 (9) {
  protected $_eventsManager =>
  NULL
  protected $_foundPath =>
  NULL
  protected $_checkedPath =>
  NULL
  protected $_classes =>
  array(0) {
  }
  protected $_extensions =>
  array(1) {
    [0] =>
     string(3) "php"
  }
  protected $_namespaces =>
  array(1) {
     'Task' =>
    array(1) {
      [0] =>
      string(36) "/home/vagrant/Code/phalcon-cli/Task"
    }
  }
  protected $_directories =>
  array(1) {
    [0] =>
    string(36) "/home/vagrant/Code/phalcon-cli/Task"
  }
  protected $_files =>
  array(0) {
  }
  protected $_registered =>
  bool(true)
}

So I'm sure loader's path is no problem.

And you sure that file name is MainTask.php ?

Yes, the file name is MainTask.php

[email protected]:~/Code/phalcon-cli/Task$ ls -al
total 5
drwxrwxrwx 1 vagrant vagrant    0 Dec 28 02:58 .
drwxrwxrwx 1 vagrant vagrant 4096 Dec 28 11:07 ..
-rwxrwxrwx 1 vagrant vagrant  396 Dec 28 02:58 MainTask.php


85.5k
edited Dec '16

it all seems fine... delete the file and create it manually without copy / pasting anything



85.5k
Accepted
answer

btw what is the reason to have both registerNamespaces and registerDirs


$loader->registerNamespaces([
    'Task'    => realpath(__DIR__."/Task/"),
])->registerDirs([
    realpath(__DIR__."/Task/"),
])->register();

?

Thanks for your remiding !
When I delete the code "namespace Task;" in Task/MainTask.php, it works perfectly.

In a word, task file no support for namespace.
Thanks for your help.