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:

<?php
(...)
$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();
$loader->registerNamespaces(
    array('SomeName\Task' => $config->application->classesDir . 'Task')
)->register();

//Create a console application
$console = new ConsoleApp();
$console->setDI($di);

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

(...)

try {
    // handle incoming arguments
    var_dump($arguments);
    $console->handle($arguments);
} catch (\Phalcon\Exception $e) {
    var_dump($e->getMessage());
    var_dump($e->getTraceAsString());
    exit(255);
}

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?



16.4k

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();



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


16.4k

Yep, worked, thanks

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


8.6k

Thanks, works now.



2.1k
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.

$application->registerModules(array(
    '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'
    ),
));