Chris
Chris

Reputation: 8968

Zend framework route

I have the following controllers in my modules..

UserController.php AdminUserController.php

Now the route for admin controller goes to: module/admin-user/ (default behaviour)

How do I make a route so all admin- will be changed to:

/admin/module/user

Upvotes: 2

Views: 951

Answers (1)

Darryl E. Clarke
Darryl E. Clarke

Reputation: 7637

You will have to write a custom route.

In code:

$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route  = new Zend_Controller_Router_Route(
    'admin/:module/user', array('controller' => 'admin-user'));
$router->addRoute('module-admin-router', $route);

In a .ini file (I like to keep mine separate from application.ini):

[routes]
routes.module-admin-router.type = "Zend_Controller_Router_Route"
routes.module-admin-router.route = "archive/:module/user"
routes.module-admin-router.defaults.controller = "admin-user"

Then you will have to bootstrap that application section to enable the routes;

protected function _initRoutes ()
{
    // setup routes here.
    $front  = $this->getResource('frontcontroller');
    $router = $front->getRouter();
    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'routes');
    $router->addConfig($config->routes);

}

This route will match any admin/module/user request and send it to the AdminUserController within the matching module.

Something like that should work. Now if things get really complicated, you'll probably need to dig into the regex router - but this is as simple as I can think of it needing to be.

Upvotes: 2

Related Questions