Frage

I am converting an old ZF app (its using an early ZF version where we used to do manual app loading/config in the index.php) to latest version, and in one of the plugin we are sending data directly to the plugin constructor

$front->registerPlugin(new My_Plugin_ABC($obj1, $obj2))

Now in the current version we can register a plugin by directly providing the details in the application.ini and I want to stay with this approach(registering using config file). So while testing, I noticed the the plugin constructor is called fairly early in the bootstrapping, so the only option I am left with is using Zend_Registry to store the data, and retrieve it in the hooks. So is it the right way? or are there any other better ways

EDIT The plugin was actually managing ACL and Auth, and its receiving custom ACL and AUTH objects. Its using the preDispatch hook.

War es hilfreich?

Lösung

Okay so you could consider you ACL and Auth handlers as a some application resources, and be able to add configuration options for them in you application.ini

 //Create a Zend Application resource plugin for each of them

 class My_Application_Resource_Acl extends Zend_Application_Resource_Abstract {
     //notice the fact that a resource last's classname part is uppercase ONLY on the first letter (nobody nor ZF is perfect)
     public function init(){
         // initialize your ACL here
         // you can get configs set in application.ini with $this->getOptions()

         // make sure to return the resource, even if you store it in Zend_registry for a more convenient access
         return $acl;
     }
 }

 class My_Application_Resource_Auth extends Zend_Application_Resource_Abstract {
     public function init(){
         // same rules as for acl resource
         return $auth;
     }
 }

 // in your application.ini, register you custom resources path
 pluginpaths.My_Application_Resource = "/path/to/My/Application/Resource/"
 //and initialize them
 resources.acl =    //this is without options, but still needed to initialze
 ;resources.acl.myoption = myvalue // this is how you define resource options

 resources.auth =    // same as before

 // remove you plugin's constructor and get the objects in it's logic instead
 class My_Plugin_ABC extends Zend_Controller_Plugin_Abstract {
     public function preDispatch (Zend_Controller_Request_Abstract $request){
          //get the objects
          $bootstrap = Zend_Controller_Front::getInstance()->getParam("bootstrap");
          $acl = $bootstrap->getResource('acl');
          $auth = $bootstrap->getResource('auth');
          // or get them in Zend_Registry if you registered them in it

          // do your stuff with these objects

     }
 }

Andere Tipps

Acl is needed so many other places hence storing it in Zend_Registry is cool thing to do and since Zend_Auth is singleton so you can access it $auth = Zend_Auth::getInstance() ; anywhere you like so no need for auth to be stored in registry .

Lastly if you have extended Zend_Acl class for your custom acl its better to make it also singleton . Then you can access acl My_Acl::getInstance(); where My_Acl is subclass of Zend_Acl .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top