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 Add String To Captured Group

Hello, I'm creating a multi-module website with a "website" module and api modules for each version of the api (e.g.: "api-v1", "api-v2"). I'm trying to setup the router to redirect to the correct version of the api by using a regular expression:

$router->add('/api/(v[0-9]+)/:controller', array(
    'module' => 'api-' . 1,
    'controller' => 2
));

However PHP parses this as an addition of strings and Phalcon then tries to load the module "api-1" which doesn't exist. I want to load the module "api-v1" instead, is it possible?

edited Sep '15

I just found this in the docs, this seems to indicate that in this application the different versions of the api would be under the same module. Is that a better a design pattern?

// Matches /api/v1/users/peter.json
$router->add(
    '/api/(v1|v2)/{method:[a-z]+}/{param:[a-z]+}\.(json|xml)',
    array(
        'controller' => 'api',
        'version'    => 1,
        'format'     => 4
    )
);

I have multimodule aplication - each module has his own Routes class and i define them in this way:

use Phalcon\Mvc\Router\Group;

class Routes extends Group{
    public function initialize()
    {
        $this->setPaths(array('module'=>'admin','namespace'=>'Samochody\Controllers\Admin'));
        $this->setPrefix('/api/admin');
        $this->addGet('/index/{page:[0-9]+$}', array('action'=>'index','controller'=>'admin'));
        $this->addGet('/index', array('action'=>'index','controller'=>'admin'));
        $this->addPost('/index/{page:[0-9]+$}', array('action'=>'index','controller'=>'admin'));
        $this->addPost('/index', array('action'=>'index','controller'=>'admin'));
        $this->addPost('/{id:[0-9]+$}', array('action'=>'edit','controller'=>'admin'));
        $this->addPost('', array('action'=>'create','controller'=>'admin'));
        $this->addDelete('/{id:[0-9]+$}', array('action'=>'remove','controller'=>'admin'));
        $this->addGet('/{id:[0-9]+$}', array('action'=>'show','controller'=>'admin'));
        $this->addGet('/roles',array('action'=>'index','controller'=>'roles'));
    }
}

For me its the best way imho cuz it looks best. And then you just mount this class in $router