views:

310

answers:

3

Hi! I'm working on multi-tenant application in Zend Framework which gets it's tenantID from the subdomain name (mod_rewrite -> index.php -> matches it against the database).

My question is - how do I set this variable (tenant id) to be available for every Controller?

Leonti

+1  A: 

I think Zend_Registry might be the way to go. http://framework.zend.com/manual/en/zend.registry.html Is this is the right way to do it?

Leonti

Leonti
+4  A: 

Yes, Zend_Registry can be used for that. Another thing you can do is registering a pre-dispatch controller plugin, which will add the tenantID as a request parameter before any controller receives it:

class YourApp_Plugin_IdWriter extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $request->setParam('tenantID', ...);
    }
}

You need to register the plugin in your application.ini:

resources.frontController.plugins.access = "YourApp_Plugin_IdWriter"
Ivan Krechetov
A: 

I think that a front controller plugin that just sets a variable is too much overhead.

A simpler way is to create your base action controller and inherits all others from it.

class MyCompany_Controller_Action extends Zend_Controller_Action
{
    public function preDispatch()
    {
        parent::preDispatch();

        $this->getRequest()->setParam('tenantId', 42);
    }
}

You have another indirect benefit that all your controllers inherits from this base one, so it's easier to add common logic that should be used from all.

Luiz Damim