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

Cli Task not found when in namespace

Hello, I have the same Problem as described here: https://forum.phalcon.io/discussion/4295/namespaced-tasks-in-cli-why-not-working

Unfortunately that thread was marked as solved by accident, but is not solved. Therefore I want to "reopen" that thread here. Here's my code:

$di = new CliDI();

// Define path to application directory
defined('CLI_PATH') || define('CLI_PATH', realpath(dirname(__FILE__)));

// Load the configuration file (if any)
if (is_readable(__DIR__ . '/../app/config/config.php')) {
    $config = include __DIR__ . '/../app/config/config.php';
} else {
    throw new \Exception('Can not read config');

$loader = new \Phalcon\Loader();
    array('SomeName\Task' => $config->application->classesDir . 'Task')

//Create a console application
$console = new ConsoleApp();

var_dump(get_class(new \SomeName\Task\MainTask())); //outputs


try {
    // handle incoming arguments
} catch (\Phalcon\Exception $e) {

The console output is is as follows:

string(23) "SomeName\Task\MainTask"
array(0) {
string(39) "MainTask handler class cannot be loaded"
string(253) "#0 [internal function]: Phalcon\CLI\Dispatcher->_throwDispatchException('MainTask handle...', 2)
#1 [internal function]: Phalcon\Dispatcher->dispatch()
#2 /home/dompie/workspace/project/app/cli.php(56): Phalcon\CLI\Console->handle(Array)
#3 {main}"

Any suggestions how I can convince phalcon to load namespaced classes from CLI?


Yeah got the same error

// Load the autoloader file (if any) if(is_readable(APPLICATION_PATH . '/../app/config/loader.php')) include APPLICATION_PATH . '/../app/config/loader.php'; $loader->registerDirs([ APPLICATION_PATH . '/tasks' ]); $loader->register();

edited Jan '15
$di->get('dispatcher')->setDefaultNamespace('SomeName\Task'); ?
$di->get('dispatcher')->setNamespaceName ('SomeName\Task'); ?


Yep, worked, thanks

$di->get('dispatcher')->setDefaultNamespace('SomeName\Task'); ?
$di->get('dispatcher')->setNamespaceName ('SomeName\Task'); ?


Thanks, works now.

edited Jan '15

i looked into the code and realised it was using the same dispatcher object. figured it was the same as using modules. so thats that.

side tip: if you guys dont already know it, you can use nested modules in phalcon. how ever many freaking layers you like.

    'module' => array(
            'className' => '\Module',
            'path' => __DIR__ . '/../apps/Module.php'
    'nested/module' => array(
        'className' => 'Nested\Module',
        'path' => __DIR__ . '/../apps/nested/Module.php'
    'nested/nested/module' => array(
        'className' => 'Nested\Nested\Module',
        'path' => __DIR__ . '/../apps/nested/nested/Module.php'