0

I have a module based zend application. I am trying to route modules appropriately, based on a version number (param) defined, ie. domain.com/api/v1.

my app looks like this

-application
...
--modules 
----default
----api
----api2
...

I have a router in my bootstrap routing correctly, but to a default module, 'api'.

    $frontController = Zend_Controller_Front::getInstance();

    $router = $frontController->getRouter();

    $route = new Zend_Controller_Router_Route(
                    'api/:apiversion/:controller/:action/*',

                    array('module' => 'api',
                        'apiversion' => ':apiversion',
                        'controller' => ':controller',
                        'action' => ':action')
    );

    $router->addRoute('api', $route);

How can i route based on the apiversion param, 'v1', 'v2'?

domain.com/api/v2/users/id/5 =>  /api2/users/id/5

-Yes i understand I can have the user input domain.com/api2/ ... -not what i want.

-Yes i understand I can have the user input domain.com/api/version/2 ... -not what i want.

I will only do those if what I want can't be achieved. Thank you :)

I figure I can do a ->getParam("v1") to see if it exists, then set the appropriate module, unless there is a pretty, dynamic way

0

1 Answer 1

1

What about doing something like that :

$frontController = Zend_Controller_Front::getInstance();

$router = $frontController->getRouter();

$route = new Zend_Controller_Router_Route(
            'api/v1/:controller/:action/*',

            array('module' => 'api',
                'apiversion' => 'v1',
                'controller' => ':controller',
                'action' => ':action')
);

$router->addRoute('api', $route);
$route2 = new Zend_Controller_Router_Route(
            'api/v2/:controller/:action/*',

            array('module' => 'api2',
                'apiversion' => 'v2',
                'controller' => ':controller',
                'action' => ':action')
);

$router->addRoute('api2', $route2);

Regards

mimiz

Sign up to request clarification or add additional context in comments.

2 Comments

hmm. tried something almost similar at first, but I was getting api:v1 resource not found issues (ACL implemented). But now it is working of course :p
Also, was trying to go for a generic version route, without defining the version number, but this will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.