Eduard Luca
Eduard Luca

Reputation: 6602

Have a default controller in Zend

I have a module-based arhitecture in Zend Framework 1.11 (site.com/module/controller/action).

I've setup my site to have a default module, so that if I have the site module as default, and you go to site.com/something1/something2, it will actually take you to site.com/site/something1/something2.

I want to achieve the same thing 1 level further: say if you go to site.com/something, it should take you to site.com/site/index/something. I'm not talking about a redirect, just a re-routing.

Would something like this be possible?

Upvotes: 1

Views: 619

Answers (1)

drew010
drew010

Reputation: 69937

If I understand correctly, it is possible and here is an example you can put in your Bootstrap:

protected function _initControllerDefaults()
{
    $this->bootstrap('frontcontroller');
    $front = Zend_Controller_Front::getInstance();

    // set default action in controllers to "something" instead of index
    $front->setDefaultAction('something');

    // You can also override the default controller from "index" to something else
    $front->setDefaultControllerName('default');
}

If you need the default action name to be dynamic based on the URL accessed, then I think you are looking for a custom route. In that case try:

protected function _initRoutes()
{
    $router = Zend_Controller_Front::getInstance()->getRouter();

    // Custom route:
    // - Matches  : site.com/foo or site.com/foo/
    // - Routes to: site.com/site/index/foo

    $route = new Zend_Controller_Router_Route_Regex(
            '^(\w+)\/?$',
            array(
                'module'     => 'site',
                'controller' => 'index',
            ),
            array(1 => 'action')
    );
    $router->addRoute('actions', $route);
}

Upvotes: 2

Related Questions