Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Cli Task not found when in namespace

Hello, I have the same Problem as described here:

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(isreadable(APPLICATIONPATH . '/../app/config/loader.php')) include APPLICATIONPATH . '/../app/config/loader.php'; $loader->registerDirs([ APPLICATIONPATH . '/tasks' ]); $loader->register();

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


Yep, worked, thanks

```php $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'