1

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

1
  • apologies should have made it clearer that was just my example url - I need it to work for all the indexController actions dynamically, when I add regex's and catches is when it starts to break the other default routers. Commented Jan 5, 2009 at 14:35

1 Answer 1

2

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.

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

2 Comments

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.
Ah -- I don't actually use the index method of another controller, in my first tiney Zend app.

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.