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 for multiple application/modules setup

I am trying to figure out how to setup CLI for multiple application configuration and would appreciate any help on this subject.

Problem

  1. I am unable to figure out how to inform CLI about multiple application configuration. It does not seem like there is an option to pass "module" name to Phalcon\CLI\Console::handle()
  2. Looking for examples of Phalcon\CLI\Router with multiple application

My current setup looks like this:

I have two applications:

  • v1 -> path: apps/kapi/v1
  • admin -> path: apps/kapi/admin

Statement

I would like to keep my tasks within their corresponding, such that

  • AdminTask would be in admin module (I have already registerNamespaces() for this in apps/kapi/admin/module.php)
  • V1Task would be in v1 module (I have already registerNamespaces() for this in apps/kapi/v1/module.php)

CLI error I get

I have included MainTask correctly in both applications.

MainTask handler class cannot be loaded

My Attempt at cli.php

<?php
define('KAPI_ENV', 'local'); // this will be set from apache vhost later on..
require_once(__DIR__ . '/../vendor/autoload.php');

class Console extends \Phalcon\CLI\Console
{
    protected function _registerServices()
    {
        $loader = new \Phalcon\Loader();
        // other NS that are not part of vendor/autoload.php
        $namespaces = require_once(__DIR__ . '/../boot/autoload_namespaces.php');
        $loader->registerNamespaces($namespaces[KAPI_ENV]);
        $loader->register();

        $di = new \Phalcon\DI\FactoryDefault\CLI();

         // Registering a router, but how to use it?
        $di->set('router', function(){
            $router = new \Phalcon\CLI\Router();
            $router->setDefaultModule('v1');

            return $router;
       });

        $this->setDI($di);
    }

    public function main()
    {
        $this->_registerServices();

        //Register the installed modules
        $this->registerModules(array(
            'admin' => array(
                'className' => 'Kapi\Admin\Module',
                'path' => '../apps/kapi/admin/Module.php'
            ),
            'v1' => array(
                'className' => 'Kapi\V1\Module',
                'path' => '../apps/kapi/v1/Module.php'
            )
        ));
    }
}

// Process the console arguments
$arguments = array();
foreach($argv as $k => $arg) {
    if($k == 1) {
        $arguments['task'] = $arg;
    } elseif($k == 2) {
        $arguments['action'] = $arg;
    } elseif($k >= 3) {
        $arguments['params'][] = $arg;
    }
}

$console = new Console();
$console->main();

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

Frontend controllers looks like this

// index.php
<?php
define('KAPI_ENV', 'local'); // this will be set from apache vhost later on..
require_once(__DIR__ . '/../vendor/autoload.php');

class Application extends \Phalcon\Mvc\Application
{
    protected function _registerServices()
    {
        $loader = new \Phalcon\Loader();
        // other NS that are not part of vendor/autoload.php
        $namespaces = require_once(__DIR__ . '/../boot/autoload_namespaces.php');
        $loader->registerNamespaces($namespaces[KAPI_ENV]);
        $loader->register();

        $di = new \Phalcon\DI\FactoryDefault();

        //Registering a router
        $di->set('router', function(){

            $router = new \Phalcon\Mvc\Router();

            // include admin routes
            include('../apps/kapi/admin/Routes.php');
            $admin = new AdminRoutes();
            $router->mount($admin);

            // include v1 routes
            include('../apps/kapi/v1/Routes.php');
            $v1 = new V1Routes();
            $router->mount($v1);

            // defaults
            $router->setDefaultModule('v1');
            $router->removeExtraSlashes(true);

            return $router;
        });

        $this->setDI($di);
    }

    public function main()
    {
        $this->_registerServices();

        //Register the installed modules
        $this->registerModules(array(
            'admin' => array(
                'className' => 'Kapi\Admin\Module',
                'path' => '../apps/kapi/admin/Module.php'
            ),
            'v1' => array(
                'className' => 'Kapi\V1\Module',
                'path' => '../apps/kapi/v1/Module.php'
            )
        ));

        echo $this->handle()->getContent();
    }
}

$application = new Application();
$application->main();

Then for each Module.php I am loading module specific namespaces and other services (db, session and so on).