Recently I ran into the problem of needing to switch the module part of a request depending on the hostname. I needed to do this because I plugged in a whitelabeling solution to our existing web application. When the request comes in, I needed to switch from the default module to the whitelabel module so that app routing would work. I’m not 100% sure that this is the best way to go for this scenario, but this is the only way I could think of doing it. I’d be interested in finding out any alternatives to this method.
I solved this problem by creating an action helper with a preDispatch() hook that would switch the module depending on the hostname. If you want the hooks to work you need to instantiate the helper in the bootstrap, otherwise it won’t work since helpers are lazy loaded.. This article has more details about action helpers, but it’s a bit dated - http://devzone.zend.com/1218/action-helpers-in-zend-framework/
Create the Action Helper
class My_Controller_Action_Helper_Whitelabel extends Zend_Controller_Action_Helper_Abstract {
public function preDispatch() {
/**
* Here we want to check whether this is a whitelabel url, and if so switch the module.
*/
if ($this->isWhitelabelUrl() && $this->getRequest()->getModuleName() == 'default') {
$this->getRequest()->setModuleName('whitelabel');
$this->getRequest()->setDispatched(false);
}
}
/**
* Checks whether the current host is a whitelabel or not
* @return boolean
*/
private function isWhitelabelUrl() {
//code to check condition
}
}
Note that ‘My’ is just a namespace I use. You can even put this inside your application folder and not in a separate library folder. You can read this question on SO for more information - http://stackoverflow.com/questions/4701177/zend-action-helper.
$this->getRequest()->setModuleName('whitelabel');
$this->getRequest()->setDispatched(false);
Switching modules only takes two lines, the first line resets the module and the second line sets the dispatched flag for the request. If you don’t set the dispatched flag to false, Zend will just serve the default module. Keep in mind that if you don’t have the conditional, Zend will simply get into a loop because it will keep trying to dispatch the request over and over again.
Now that you have the action helper created, you need to hook it into the bootstrap file so that an instance of the helper is loaded when your application is loaded. You need to do this because you need the preDispatch() hook to be fired during the normal routing process.
Initialize in Bootstrap
In my Bootstrap.php file, I have a _initApplication() method which is fired when the application is loaded. I’ve put the instantiation code for the helper in that method.
public function _initApplication() {
//code
Zend_Controller_Action_HelperBroker::addHelper(
new My_Controller_Action_Helper_Whitelabel()
);
}
This creates an instance of our whitelabel helper and Zend will execute the code in the hooks at the proper time.
Something I have to look into is, how routing is affected when I set ‘dispatched’ to false. I haven’t found enough information on this yet, but will update this space if and when I find more.
