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

API versioning problem

I have multi-module phalcon app (api, admin and others) Now I want to add versioning to API module. Here my FS structure

---v1 files
---v2 files

I got a problem with $loader->registerNamespaces and $router->addModuleResource (Annotation routing) Namespaces should be registered before adding a route, but when I trying to addModuleResource inside Dispatcher - they are not applying.

Is there a place where I can get router->getRewriteUri() and then add routes based on url which contain api version?

And again: I want to register namespaces and add routes after I know api version from URI


When you installed the phalcon in your system? What are the infos about your system, server, phalcon version, etc?

edited Jul '16

Phalcon 2.1.0b, PHP 5.6, Ubuntu 14.04

I decided to do it before module loading using $_SERVER[REQUEST_URI]

i have some problems with phalcon last stable version, if you suppose this is the problem try install phalcon/legacy and try again. I solved my problem with sessions wich don't work in phalcon 2.x

sudo apt-add-repository ppa:phalcon/legacy
sudo apt-get update
sudo apt-get install php5-phalcon


Hey Dima, I've also had your questions few weeks ago and i ended with the following structure:

-- frontend
---- ....

-- backend
---- ....

-- api
---- v1
------ controllers/
------ Module.php
---- v2
------ controllers/
------ Module.php

My URL structure is like:


And I capture the ApiVersion in the bootstrap file with url parsing. And in the Module.php file you can set version specific routes, services e.t.c.

$modules['api'] = [
    'className' => 'Api\Module',
    'path' => __DIR__ .'/api/'. $this->app->config->apiVersion .'/Module.php'
edited Jul '16

Thank you for an idea! Your solution is pretty good. My code has turned a little bit messy with a dynamic includes, but I needed to handle my API calls within 1 single module. Anyway thank you for a tip!