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 (http://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!



67.7k

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

https://docs.phalconphp.com/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



48.4k

I extended the Router class to provide a setDefaultModule and addStdModule method. This allows the benefit of having certain modules behave boring and standardly while allowing complex and special pretty routing in other modules like for a single page application.

Also you will see that it addStdModule does a lot of tedious stuff like defining the necessary four routes.

$router->setDefaultModule('web');
$router->addStdModule('admin');
$router->addStdModule('api');

Router:

<?php
namespace Webird\Mvc;

use Phalcon\Mvc\Router as PhRouter;

class Router extends PhRouter
{

    /**
     *
     */
    private $defaultModule;

    /**
     *
     */
    public function getDefaultModule()
    {
        return $this->defaultModule;
    }

    /**
     *
     */
    public function setDefaultModule($moduleName)
    {
        parent::setDefaultModule($moduleName);
        $this->addStdModule($moduleName);
        $this->defaultModule = $moduleName;
    }

    /**
     *
     */
    public function addStdModule($moduleName)
    {
        $this->add("/$moduleName", [
            'module' => $moduleName,
            'controller' => 'index'
        ]);
        $this->add("/$moduleName/:controller", [
            'module' => $moduleName,
            'controller' => 1
        ]);
        $this->add("/$moduleName/:controller/:action", [
            'module' => $moduleName,
            'controller' => 1,
            'action' => 2,
        ]);
        $this->add("/$moduleName/:controller/:action/:params", [
            'module' => $moduleName,
            'controller' => 1,
            'action' => 2,
            'params' => 3
        ]);
    }
}