Zend Framework - pass a variable to each controller

I am working on a multi-user application in the Zend Framework that gets its tenantID on behalf of a subdomain (mod_rewrite -> index.php -> matches it in the database).

My question is: how to set this variable (tenant ID) for each controller?

Leonti

+4
source share
3 answers

Yes, Zend_Registry can be used for this. Another thing you can do is register the controller plugin before sending, 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 application:

 resources.frontController.plugins.access = "YourApp_Plugin_IdWriter" 
+9
source

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

Leonti

+1
source

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

The simplest way is to create your own basic action controller and inherit all the rest 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 inherit from this basic one, so it’s easier to add a common logic that should be used from all.

0
source

All Articles