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

Module Routing Not Working as Expected

Hey there,

I'm writing an API app, complete with a versioning strategy. I found something really weird happening in the routing of modules. I posted originally at stack overflow (https://stackoverflow.com/questions/34170713/module-routing-not-right/34172068), but I didn't get an answer, so much as I got hacks (I mean no disrespect to the help I received; I greatly appreciate the help).

Here's the problem:

  • /v1 | /v1/ - Doesn't work
  • /v1/users | /v1/users/ - Works fine
  • /v1/users/1 | /v1/users/1/ - Works fine
  • /v1/users/1/permissions | /v1/users/1/permissions - Works fine

Here's the router itself:

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

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

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

        $router->removeExtraSlashes(true);

        return $router;
    });

At this point, I was hoping to get an explanation as to why this problem exists. I can't seem to figure out why the module doesn't match to the route. The rest of the routes work fine; it's just that first one that doesn't match. I am not looking for a solution; I wanted to get a converstaion going on why this is happening, and, if it's a potential problem, maybe talk about how we could fix it.

Any guidance would be great!

Thanks!



85.5k

you can try with groups, this is how i did it and it works for me

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Router_Group.html

this is one of the phalcon's weirdness and most likely a bug; but I didn't find to be a big deal, so I got over it like many others around here.

If Izo's solution doesn't work or is not what you're looking for, you can submit a bug report

https://github.com/phalcon/cphalcon/issues

dasfisch editor Hey there,

I'm writing an API app, complete with a versioning strategy. I found something really weird happening in the routing of modules. I posted originally at stack overflow (https://stackoverflow.com/questions/34170713/module-routing-not-right/34172068), but I didn't get an answer, so much as I got hacks (I mean no disrespect to the help I received; I greatly appreciate the help).

along with your existing route for the module you could also add route for accessing only by module name.