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: http://forum.phalconphp.com/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?



11.6k

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



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


11.6k

Yep, worked, thanks

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



5.4k

Thanks, works now.



1.5k
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'
    ),
));