我正在将一个旧的ZF应用程序(它使用我们曾经在index.php中手动应用程序加载/配置的早期ZF版本)转换为最新版本,并且在其中一个插件中我们将数据直接发送到插件构造函数
$front->registerPlugin(new My_Plugin_ABC($obj1, $obj2))
现在在当前版本中,我们可以直接在application.ini中提供详细信息来注册插件,我想继续使用这种方法(使用配置文件注册)。所以在测试时,我注意到插件构造函数在引导过程中很早就被调用了,所以我唯一的选择是使用Zend_Registry存储数据,并在钩子中检索它。这是正确的方法吗?还是有其他更好的方式
修改 该插件实际上是管理ACL和Auth,以及它接收自定义ACL和AUTH对象。它使用preDispatch挂钩。
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
Acl需要很多其他地方因此将它存储在Zend_Registry中是很酷的事情,因为Zend_Auth是单身,所以你可以访问它$auth = Zend_Auth::getInstance()
;你喜欢的任何地方,所以不需要将auth存储在注册表中。
最后,如果你为自定义acl扩展了Zend_Acl类,那么最好使它也是单例。然后你可以访问acl My_Acl::getInstance();
,其中My_Acl是Zend_Acl的子类。