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

Using specific routes inside each module

I'm new with Phalcon. I just wanted to know if is this possible to define routes inside each modules separately?

edited Mar '14

I meant is this the best way? Or can tell me if you have you have better idea?

services.php
...
$di['router'] = function () {

    $router = new Router();

    $router->setDefaultModule("frontend");
    $router->setDefaultNamespace("Sample\Frontend\Controllers");

    $modules = scandir(__DIR__ . '/../apps/');
    foreach ($modules as $module) {
        if (file_exists(__DIR__ . '/../apps/' . $module . '/config/routes.php')) {
            include __DIR__ . '/../apps/' . $module . '/config/routes.php';
        }
    }

    return $router;
};
...
routes.php
...
    $router->add(
        "/index",
        [
            'namespace'  => 'Sample\Backend\Controllers',
            'module'     => 'backend',
            'controller' => 'index',
            'action'     => 'index',
        ]
    );
...


98.9k

A router must know in advance all routes in the application, so it can forward to the right module.

  • Read URI info
  • Load Routes
  • See what route is matched according to the current URI info
  • Dispatch module -> controller

Regarding application organization, your approach is a good option.

edited Mar '14

I use this method: in my application bootstrap:

<?php
        // Routing
        $router = new Phalcon\Mvc\Router(false);

        $router->setDefaultModule('site');
        $router->setDefaultController('index');
        $router->setDefaultAction('index');

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

        foreach ($application->getModules() as $module) {
            $routesClassName = str_replace('Module', 'Routes', $module['className']);
            if (class_exists($routesClassName)) {
                $routesClass = new $routesClassName();
                $router      = $routesClass->add($router);
            }
        }
        $router->removeExtraSlashes(true);

        $di->set('router', $router);

in my module directory (eg. Backend module): "backend/Routers.php"

<?php
namespace Backend;

class Routes
{
    public function add($router)
    {
        $backend = new \Phalcon\Mvc\Router\Group(array(
            'module'     => 'backend',
            'controller' => 'index',
            'action'     => 'index'
        ));
        $backend->setPrefix('/backend');

        $router->add('/backend(/:controller(/:action(/:params)?)?)?', array(
            'module'     => 'backend',
            'controller' => 2,
            'action'     => 4,
            'params'     => 6,
        ))->setName('backend');

        $backend->add('/', array(
            'action' => 'index',
        ))->setName('backendhome');

        $router->mount($backend);
        return $router;
    }
}