views:

704

answers:

2

I'm trying to map an action to the base URL and default controller.

Swear this should be straight forward or even an 'out the box' feature but how do you map actions in your default controller to the base url using the Zend Framework? I'm fairly new to the Framework so I'm hoping I'm just missing something obvious.

Trying to map:

domain.com/index/my-page to domain.com/my-page

without breaking any of the other routes setup by default.

I can hack it using the Zend_Router but it breaks the other rules e.g. /:controller/:action /:module/:controller/:action

note: /index/my-page is an example url - I need it to work for all the indexController actions dynamically.

URL Mapping examples as requested by 'tharkun' indexController has methods indexAction and contactAction need urls

/index
/index/contact
/
/contact

2nd controller testController has methods indexAction and monkeyAction need urls

/test
/test/index
/test/monkey

basically - if the sys can't find a controller of VAR then it looks for a action of VAR in the default controller

A: 

domain.com/index/my-page to domain.com/my-page

how do you think this should work?

in "domain.com/my-page" my-page is not an action but a controller so if there is no my-page controller with an index action you get a 404.

if the rule is /:controller/:action then that's the rule.

why don't you use vars? if you explain more clearly what exactly you want to do, ppl might be able to come up with practical solutions.

tharkun
+2  A: 

The default controller is IndexController.

The (default) mapping works like this:

/ => IndexController::indexAction
/index => IndexController::indexAction
/index/foo => indexController::fooAction
/foo => FooController::indexAction

So, add a user defined route like this (will have lower priority than the default)

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
    '/:action',
    array(
        'controller' => 'index'
    )
);
$router->addRoute('user', $route);

This did not break my use of the default routes.

Edit: As coffeerings said in the comment, this will break the indexAction of non-default controllers.

gnud
nearly :) it works for all but the default actions in other controllers. so indexController methods /, /index and /testme work but in testController only /test/index and /test/testme work the default /test/ action fails.
coffeerings
Ah -- I don't actually use the index method of another controller, in my first tiney Zend app.
gnud