2011-09-23 42 views
0

我正在將一箇舊的ZF應用程序(它使用早期的ZF版本,我們用於在index.php中手動應用程序加載/配置)轉換爲最新版本,並在一個該插件的,我們直接到插件的構造函數在當前版本中發送數據現在如何將數據/變量/對象傳遞給Zend_Controller_Plugin

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

,我們可以通過直接的application.ini提供詳細信息註冊一個插件,我想留在這個方法(使用登記配置文件)。所以在測試時,我注意到插件構造函數在啓動時相當早,所以我唯一的選擇是使用Zend_Registry存儲數據,並在鉤子中檢索它。那麼這是否正確?或者還有沒有其他更好的方法

編輯 該插件實際上是管理ACL和Auth,並且它的接收自定義ACL和AUTH對象。它使用preDispatch鉤子。

+0

答案將取決於三個主要的東西,你將需要提供有關你的問題的細節:你準確地*傳遞給這個構造函數,這個插件的目的是什麼,以及流程在哪個狀態行爲(routeStartup,routeShutdown,predispatch等)。 –

+0

好的,更新了答案 –

回答

0

好了,你可以考慮你ACL和驗證處理的一些應用程序資源,並能夠添加配置選項,它們在你的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 

    } 
} 
0

的Acl需要其他許多地方,因此存儲它在Zend_Registry中是很酷的事情,因爲Zend_Auth是單例,所以你可以訪問它$auth = Zend_Auth::getInstance();你喜歡的任何地方,所以不需要auth存儲在註冊表中。

最後,如果您爲自定義acl擴展了Zend_Acl類,那麼最好使它成爲單例。然後,您可以訪問acl My_Acl::getInstance();,其中My_Acl是Zend_Acl的子類。