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

Router\Group behavior

Good afternoon. I'm using Phalcon 2.0.6.

Could you please explain to me the behavior of the Router\Group class. I have a multimodule application. Each module is on the same domain (e.g. domain.dev/...domain.dev/admin...). I want to specify the groups of the routes for each module and to store it in separate files. The problem is that if i have routes like "/:controller" for the main module then the route "/admin/:controller" for the second module doesn't fire.

Routes files example:

namespace NAMESPACE_TO\Routes;

class Site extends \Phalcon\Mvc\Router\Group {

    public function initialize() {

        $this->setPaths(array(
            'module' => 'site'
        ));

        $this->add('/', array(
            'controller' => 'index',
            'action' => 'index'
        ));

        $this->add('/:controller', array(
            'controller' => 1,
            'action' => 'index'
        ));

        $this->add('/:controller/:action/:params', array(
            'controller' => 1,
            'action' => 2,
            'params' => 3
        ));
    }
}
namespace NAMESPACE_TO\Routes;

class Panel extends \Phalcon\Mvc\Router\Group {

    public function initialize() {

        $this->setPaths(array(
            'module' => 'panel'
        ));

        $this->add('/admin', array(
            'controller' => 'index',
            'action' => 'index'
        ));

        $this->add('/admin/:controller', array(
            'controller' => 1,
            'action' => 'index'
        ));

        $this->add('/admin/:controller/:action/:params', array(
            'controller' => 1,
            'action' => 2,
            'params' => 3
        ));
    }
}

Router service definition:

$di->set('router', function () use ($config) {
    $router = new Router(false);
    $router->removeExtraSlashes(true);

    return $router
        ->mount(new PanelRoutes())
        ->mount(new SiteRoutes());
        //...more modules...
}, true);

After that the route for the "site" module always fires. I also tried to use setPrefix() function for every route group. Is it possible to define the behavior for each group? Or I can only specify the static routes like "/save", "/admin/something" ?



34.6k
Accepted
answer

Change the order in which groups are registered so the ones using /admin are handled first by the router.

Thanks, it works