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

Routing in multi-module application

Hello!

I'm using a multi-module application template and I ran into this problem. At first I wanted to make all calls to the API into a separate module and make all calls to it. But, now I understand that this approach will destroy the encapsulation of modules. Of course, you can do IPI for each module separately, but in this case, you get a large amount of router rule (for each module there are now 3 rules, with the API will be at least 4, there are a lot of modules planned).

Can I somehow solve the problem with a lot of rules for the router?

Now the initialization of the router looks like this:

public function register()
{
    $this->di->setShared(
        $this->serviceName,
        function () {
            $router = new Router();

            $router->notFound([
                'module' => 'cabinet',
                'controller' => 'error',
                'action' => 'show404'
            ]);

            $router->add("/:controller/:action/:params", array(
                'module' => 'cabinet',
                'controller' => 1,
                'action' => 2,
                'params'=>3
            ))->convert('action', function ($action) {
                return Text::lower(Text::camelize($action));
            });

            $router->add("/", array(
                'module' => 'cabinet',
                'controller' => 'index',
                'action' => 'index',
            ));

            $router->add("/api", array(
                'module' => 'api',
                'controller' => 'index',
                'action' => 'index',
            ));

            $router->add("/api/:controller/:action/:params", array(
                'module' => 'api',
                'controller' => 1,
                'action' => 2,
                'params' => 3,
            ))->convert('action', function ($action) {
                return Text::lower(Text::camelize($action));
            });

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

            $router->add("/admin/:controller", array(
                'module' => 'admin',
                'controller' => 1,
                'action' => 'index',
            ))->convert('action', function ($action) {
                return Text::lower(Text::camelize($action));
            });

            $router->add("/admin/:controller/:action/:params", array(
                'module' => 'admin',
                'controller' => 1,
                'action' => 2,
                'params'=>3
            ))->convert('action', function ($action) {
                return Text::lower(Text::camelize($action));
            });
    }


32.3k
Accepted
answer

Hi @dmitrichev you can get a little small router file using :module in your routes to match the name of the module. Additionaly I have a Route.php for each module when I extend Phalcon\Mvc\Router\Group but as the routing is tedious.

Route group docs

Good luck

Hi @emiliodeg! Thanks for the answer. This will solve the problem.