0

我正在将旧的 ZF 应用程序(它使用早期的 ZF 版本,我们曾经在 index.php 中进行手动应用程序加载/配置)转换为最新版本,并且在其中一个插件中,我们将数据直接发送到插件构造函数

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

现在在当前版本中,我们可以通过直接在 application.ini 中提供详细信息来注册插件,我想继续使用这种方法(使用配置文件注册)。所以在测试时,我注意到插件构造函数在引导的早期就被调用了,所以我剩下的唯一选择是使用 Zend_Registry 来存储数据,并在挂钩中检索它。那么这是正确的方法吗?或者有没有其他更好的方法

编辑 该插件实际上是管理 ACL 和 Auth,以及它接收自定义 ACL 和 AUTH 对象。它使用 preDispatch 钩子。

4

2 回答 2

0

很多其他地方都需要 Acl,因此将它存储在 Zend_Registry 中是一件很酷的事情,因为 Zend_Auth 是单例的,所以你可以访问它$auth = Zend_Auth::getInstance();您喜欢的任何地方,因此无需将身份验证存储在注册表中。

最后,如果您为自定义 acl 扩展了 Zend_Acl 类,最好将其设为 singleton 。然后您可以访问 acl My_Acl::getInstance(); ,其中 My_Acl 是 Zend_Acl 的子类。

于 2011-09-24T02:36:36.200 回答
0

好的,您可以将 ACL 和 Auth 处理程序视为一些应用程序资源,并能够在 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

     }
 }
于 2011-09-23T23:10:21.330 回答