Skip to content
phalcon edited this page Aug 8, 2012 · 6 revisions

Here's the Multi-Module applications proposal.

Let's pretend we have a directory structure like this:

apps/
   default/
     config/
     controllers/
     models/
     views/
     Module.php
   backend/
     config/
     controllers/
     models/
     views/
     Module.php
   frontend/
     config/
     controllers/
     models/
     views/
     Module.php
public/
   js/
   css/
   index.php

There are a directory called "apps" which contains applications or modules. Each of them have the usual MVC directories. The Module.php file contains a Module definition, for instance:

apps/backend/Module.php:

<?php

namespace Store\Backend;

use Phalcon\Mvc\ModuleDefinition;
use Phalcon\Loader as Loader;
use Phalcon\Config\Adapter\Ini as Config;

//a module implements the ModuleDefinition interface, ensuring that registerAutoloaders and getConfig are  implemented
class Module implements ModuleDefinition {

    //This method register autoloaders for MVC locations and other user stuff
    public function registerAutoloaders(){

        $loader = new Loader();

        $loader->registerNamespaces(array(
            'Store\Backend\Controllers' => 'apps/backend/controllers/'
            'Store\Backend\Models' => 'apps/backend/models/'
        ));

        $loader->register();

    }

    //This method returns the configuration object for this module
    public function getConfig(){
        return new Config('app/backend/config/config.ini');
    }

}

Now the bootstrap, the idea is introduce a new component Phalcon\Mvc\Orchestrator, which allows to register module definitions, a router will indicate which module/controller/action should be executed:

public/index.php: <?php

use Phalcon\Router\Regex as Router;
use Phalcon\Mvc\Orchestrator as MvcOrchestrator;

try {

    // Create a router
    $router = new Router();

    //register some routes defining module/controller/action
    $router->add("/admin/products/:action", array(
        'module' => 'backend',
        'controller' => 'Store\Backend\Controllers\Products',
        'action' => 1
    ));

    $router->add("/welcome", array(
        'module' => 'default',
        'controller' => 'Store\Default\Controllers\Index',
        'action' => 'index'
    ));

    //this class orchestrate the entire MVC flow
    $orchestrator = new MvcOrchestrator();

    //set the router
    $orchestrator->setRouter($router);

    //register the modules and their definitions
    $orchestrator->registerModules(array(
        'default' => '../apps/default/Module.php',
        'backend' => '../apps/backend/Module.php',
        'frontend' => '../apps/frontend/Module.php' 
    ));

    $orchestrator->handle();   

}
catch(Phalcon\Exception $e){
    echo $e->getMessage();
}

A controller would look like this:

apps/backend/controllers/ProductsController.php <?php

namespace Store\Backend\Controllers;

use Phalcon\Mvc\Controller;

class ProductsController extends Controller
{

    public function indexAction(){

    }

}

General considerations:

Directories for models and controllers disappear as requirements of the framework. In reality, controllers and models could be located anywhere. They will be instantiated via autoloaders.
The directory structure is now completely flexible.
The use of namespaces prevent naming collisions allowing the same controller exists in different modules.
The definition of the module allows you to register an autoloader per module.
Is simple and uses the  available components in the framework!

if you agree, we can start immediately with this and not waste any more time, thoughts?

Clone this wiki locally