We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

independent routers for each module (multi app)

Is there a way to load a module based on a parameter from a uri and then parse uri firther with separate router for each module? I've read through tutorial and as i got it, routes / grouping allows to have only one router and i did not find any way to make stepped desicions (first define module, than pass uri to that module and continue routing) Am i missing something?



2.4k
edited Jun '15

you mean, i should build micro application on top of my multi-mvc app? I don't want to move routing logic into controllers... And don't think it somehow related to generating urls...

Maybe, i misstell my task. What i really want is something like ability to define which module should take care of request before loading a module:

index.php:


    $config = include __DIR__ . "/../var/config/config.php";
    include __DIR__ . "/../var/config/loader.php";
    include __DIR__ . "/../var/config/services.php";

    $di->set('router', function(){
        $oRouter = new Router();

        $oRouter->add('/api/.*', array(
            'module' => 'Api'
        ));
        $oRouter->add('/regular/.*', array(
            'module' => 'Regular'
        ));

        return $oRouter;
    });

    $application = new \Phalcon\Mvc\Application($di);

    $application->registerModules(array(
        'regular' => array(
            'className' => 'Modules\Regular\Module',
            'path' => '../app/modules/regular/Module.php',
        ),
        'api' => array(
            'className' => 'Modules\Api\Module',
            'path' => '../app/modules/api/Module.php',
        ),
    ));

    echo $application->handle()->getContent();

...comments in code seemed to brake all the markdown, so i get rid of it.

And than somehow add concrete routes in Module.php of corresponding module

I think you mean event manager. The code is like

    /*
     * Event Manager
     */
    //Create a events manager
    $eventManager = new Phalcon\Events\Manager();
    //Listen all the application events
    $eventManager->attach('micro:afterExecuteRoute', new HeaderListener());

    /*
     * Handle the request
     */
    $app = new Phalcon\Mvc\Micro();
    // Bind the events manager to the app
    $app->setEventsManager($eventManager);

and

/**
 * Authentication API
 */
class AuthenticationListener
{

    public function beforeExecuteRoute($event, $app)
    {
        if (!PASS) return false;
    }

}

A dinamic solution to match module, controller, action, params. Is this what you need?

$router->add('/:module/:controller/:action/:params', array(
            'module' => 1,
            'controller' => 2,
            'action' => 3,
            'params' => 4
));


2.4k

A dinamic solution to match module, controller, action, params. Is this what you need?

$router->add('/:module/:controller/:action/:params', array( 'module' => 1, 'controller' => 2, 'action' => 3, 'params' => 4 ));

Nope, this is a sample code from tutorial, i've already saw it. It does not fits me as soon as it is actually not as dynamic as one may suppose.

First, it bounds concrete URI patterns to a concrete set of actions within module. So, i have to organize each module in the same way... and where is the 'module' thing if they are similar?

Second, i should write all these routes in one place, seemingly, before instantiation of an application. And it turns out, that my application is aware of what is going on in modules, which is wrong.

Third, coming after the second, module got no control of itself, and there is no possibillity to divide module's logic and keep it separate.



2.4k

I think you mean event manager. The code is like

   /*
    * Event Manager
    */
   //Create a events manager
   $eventManager = new Phalcon\Events\Manager();
   //Listen all the application events
   $eventManager->attach('micro:afterExecuteRoute', new HeaderListener());

   /*
    * Handle the request
    */
   $app = new Phalcon\Mvc\Micro();
   // Bind the events manager to the app
   $app->setEventsManager($eventManager);

and

/**
* Authentication API
*/
class AuthenticationListener
{

   public function beforeExecuteRoute($event, $app)
   {
      if (!PASS) return false;
   }

}

I feel like you're telling me about some feature i may make use of. But i just can't understand how to incorporate your suggestion of micro-mvc into my multi-mvc app.. ...d'oh. Seemes, like i should mount required module on beforeExecuteRoute, and define module routes in it.