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 a multimodule environment

Hi everyone, one of the features I liked a lot of other frameworks, for instance Zend Framework 2, is the capability to setup modules routes within the module configuration itself and eventually extends route configurations from other modules.

It is possible to do something like this in Phalcon?

Let's split this question in two parts:

1) It is possible to do route configuration for module an not to put everything in the index.php? If yes, what are the best practices?

2) It is possible to extends route configs from othe registered modules?

Add clear examples if possible.

Thanks



12.2k
edited Dec '14

Hi, I made something like this in file service.php

<?php
$di = new FactoryDefault();
....
/**
 * Router
 */
$di->set(
    'router',
    function () {
        //return include __DIR__ . "/../../app/config/routes_annotation.php";
        return include __DIR__ . "/../../app/config/routes.php";
    },
    true
);
....

After that in index.php I did something like that

    /**
     * Read services
     */
    include __DIR__ . "/../app/config/services.php";

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);

    //Register the installed modules
    $application->registerModules(array(
        'frontend' => array(
            'className' => 'APP\Frontend\Module',
            'path' => '../app/modules/frontend/Module.php'
        ),
        'backend' => array(
            'className' => 'APP\Backend\Module',
            'path' => '../app/modules/backend/Module.php'
        )
    ));

In file where you register services for module you will have acess to route service and there you can extend whatever you want.


public function registerServices($di)
    {

        $dispatcher = $di->get('dispatcher');

        $router = $di->get('router');

        $router->add(...);

    }

I've made something similar but for the views. Hope it will help or not )



43.9k

Hi,

If I understand, you want to define your module's route in each module itself and not in a gloal router.php ...

so after I had a look at this, I think that maybe adding $di['router'] definitions in public function registerServices($di) will do the trick.

Hi guys, I've tried to place route configuration inside the 'registerServices' method of the module, but this is not even called if I remove route configs from the index.php.

I would like to accomplish this task in order to have the freedom to attach/detach modules with all their rules.

Any other ideas?

Thanks



43.9k

@Sergio:

I think that I was not in the right direction. While following @Arius link I found that:

taken from: https://vegas-cmf.github.io/1.0/guide/mvc/module.html#new_module


mkdir app/modules/Foo/config
touch app/modules/Foo/config/config.php
touch app/modules/Foo/config/routes.php

// in routes.php

return array(
    'foo' => array(
        'route' => '/foo',
        'paths' => array(
            'module' => 'Foo',
            'controller' => 'Frontend\Foo',
            'action' => 'index'
        )
    )
);

then, in router service registration, you can load all the module's routes in a loop (you may a directory iterator).

edited Dec '14

I understand, so I will try to wrap Phalcon\Mvc\Application in a custom Application class that will merge all registered modules configs.

Thanks!