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 Apps with namespaces in Phalcon\Loader

Hio,

currently we are building up a CLI app. I prepared a standard bootstrap file and there everything which is need gets loaded.

In this file is a function:

   /**
     * Initializes the loader
     */
    private function _initLoader(){
        if(count($this->_namespaces) > 0){
            $loader = new Phalcon\Loader();
            $loader
                ->registerNamespaces($this->_namespaces)
                ->register();
        }
    }

The content of the $this->_namespaces looks like:

array(
    'Path/To/Ns' => 'Path\To\Ns'
);

The task itself looks like:

namespace Path\To\Ns;

class AnyTask extends Phalcon\CLI\Task{

    public function mainAction(){
        echo "Main action ...";
    }

    public function createAction(){
        echo "Create action ...";
    }

}

I receive all the time the following message when I run my app:

php -f path/to/bootstrap.php any
# Output: AnyTask handler class cannot be loaded

Any ideas?

I'm not sure, but maybe the problem is in slashes. Namespace slashes is backslash() and path slashes is usual slash(/) Shouldn't is be like this?

array(
    'Path\To\Ns' => 'Path/To/Ns'
);

Maybe it'll help, if not unfortunately I haven't any idea.



98.9k

I also think the problem is the wrong namespace separator in the loader

Sorry, my mistake here. It should be for sure:

array(
'Path\To\Ns' => 'Full/Path/To/Ns'
);

But anyway, it does not work for me.

Here is the real one to make it more clear :)

array(1) {
  ["Db\Migration\CLI\Task"]=>
  string(94) "/var/deploy/local/services.app/1369397451-0.1.0.0/src/services/lib/Db/Migration/CLI/Task"
}

Maybe now any ideas?



98.9k

Check this example:

bootstrap.php:

<?php

use Phalcon\Loader,
    Phalcon\CLI\Console,
    Phalcon\DI\FactoryDefault\CLI as CliDI;

$loader = new Loader();

$loader->registerNamespaces(array(
    'Db\Migration\CLI\Task' => __DIR__ . '/tasks/'
))->register();

$di = new CliDI();

$app = new Console();

$app->setDI($di);

$app->handle(array('task' => 'Db\Migration\CLI\Task\Any'));

tasks/AnyTask.php:

<?php

namespace Db\Migration\CLI\Task;

class AnyTask extends \Phalcon\CLI\Task
{

    public function mainAction()
    {
        echo "Main action ...";
    }

    public function createAction()
    {
        echo "Create action ...";
    }

}


31.3k

Hi I'm having the same kind of problem but with my models :

$loader = new \Phalcon\Loader();

$loader->registerNamespaces(array(

    'Tuango\CLI\Task' => APPLICATION_PATH . '/tasks',
    'Tuango\CLI\Security' => APPLICATION_PATH . '/lib/Tuango/CLI/Security/',
    'Tuango\Models' => APPLICATION_PATH . '/models/Tuango/Models/'
));

$loader->registerDirs(

    array(

        //APPLICATION_PATH . '/tasks',
        //APPLICATION_PATH . '/models'
    )
);

$loader->register();

Parent class :

namespace Tuango\CLI\Security;

abstract class Fraud  {
    //Abstract stuff here
}

Child class :

namespace Tuango\CLI\Security;

use Tuango\CLI\Security\Fraud;

class LiveFraud extends Fraud {
    //Class stuff and abstract method inherited
}

Task :

namespace Tuango\CLI\Task;

use Tuango\CLI\Security\LiveFraud;

class FraudTask extends \Phalcon\CLI\Task {

    public function mainAction() {

        $liveFraud = new LiveFraud();
        $liveFraud->execute();
    }
}

Fatal error: Class 'Tuango\CLI\Security\Fraud' not found in /Library/WebServer/Documents/tuangocli/app/lib/Tuango/CLI/Security/LiveFraud.php on line 30

I can't figurate out what's the problem here.

Any help would be appreciated :)

Thank you.



1.1k

here is my idear

$di->setShared('dispatcher', function () {
    $dispatcher = new Phalcon\CLI\Dispatcher();
    $dispatcher->setDefaultNamespace('Db\Migration\CLI\Task');
    return $dispatcher;
});