stagl
stagl

Reputation: 521

routing all module traffic to default module in Zend Framework

I'm looking to forward all of my requests from :

.com/m/

to

.com/

I thought I could try to do this in the module's bootstrap, but the front controller isn't setup just yet. I keep seeing mentions of front controller plugins, but how would I set it up just for this module?

Sorry for the dumb questions, I'm still trying to get a grasp on the Zend Framework.

Upvotes: 0

Views: 103

Answers (1)

drew010
drew010

Reputation: 69977

Here is a controller plugin that routes all traffic to a specific module to the default module. I give 2 ways to route the traffic, either by forwarding the request (url stays the same but executes default module), or by then redirecting the browser to the default module.

Note, this is untested but should work. Let me know if you have questions or problems with it.

<?php

class Application_Plugin_ModuleRedirector extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $module     = $request->getModuleName();
        $controller = $request->getControllerName();
        $action     = $request->getActionName();

        // don't execute plugin if not in the module "m"
        if ($module != 'm') {
            return ;
        }

        // foward to default module with same controller and action
        $request->setModuleName('default')
                ->setControllerName($controller)
                ->setActionName($action);

        // OR remove the above and use this for a hard redirect
        $urlHelper  = new Zend_View_Helper_Url();
        $url        = $urlHelper->url(array(
                                       'module' => 'default',
                                       'controller' => $controller,
                                       'action'     => $action));

        $redirector = Zend_Controller_Action_HelperBroker::
                      getStaticHelper('redirector');

        $redirector->gotoUrl($url);
    }
}

To activate it, register the plugin with the Front Controller:

Zend_Controller_Front::getInstance()->registerPlugin(new Application_Plugin_ModuleRedirector());

Upvotes: 1

Related Questions