Skip to content
phalcon edited this page Aug 7, 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/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

Clone this wiki locally