Zend framework 如何将数据/变量/对象传递给Zend_控制器_插件
我正在使用早期的ZF版本将旧的ZF应用程序转换为最新版本,在早期的ZF版本中,我们在index.php中手动加载/配置应用程序,在其中一个插件中,我们将数据直接发送到插件构造函数Zend framework 如何将数据/变量/对象传递给Zend_控制器_插件,zend-framework,zend-controller-plugin,Zend Framework,Zend Controller Plugin,我正在使用早期的ZF版本将旧的ZF应用程序转换为最新版本,在早期的ZF版本中,我们在index.php中手动加载/配置应用程序,在其中一个插件中,我们将数据直接发送到插件构造函数 $front->registerPlugin(new My_Plugin_ABC($obj1, $obj2)) 现在在当前版本中,我们可以通过直接在application.ini中提供详细信息来注册插件,我想继续使用这个方法使用配置文件注册。所以在测试过程中,我注意到插件构造函数在引导过程中很早就被调用了,所以
$front->registerPlugin(new My_Plugin_ABC($obj1, $obj2))
现在在当前版本中,我们可以通过直接在application.ini中提供详细信息来注册插件,我想继续使用这个方法使用配置文件注册。所以在测试过程中,我注意到插件构造函数在引导过程中很早就被调用了,所以我剩下的唯一选择就是使用Zend_注册表来存储数据,并在钩子中检索数据。那么这是正确的方法吗?或者还有其他更好的方法吗
编辑
该插件实际上是在管理ACL和Auth,并接收自定义ACL和Auth对象。它使用预调度钩子。 好的,这样您可以将ACL和AuthHAND作为一个应用程序资源来考虑,并且可以在应用程序中为它们添加配置选项。
//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
}
}
Acl需要很多其他地方,因此将其存储在Zend_注册表中是一件很酷的事情,因为Zend_Auth是单例的,所以您可以访问它$Auth=Zend_Auth::getInstance;任何您喜欢的地方,都不需要将auth存储在注册表中
最后,如果您已经为您的定制Acl扩展了Zend_Acl类,那么最好将其也设置为单例。然后可以访问acl My_acl::getInstance;其中My_Acl是Zend_Acl的子类 答案将取决于您需要在问题中提供详细信息的三个主要方面:您正将什么传递给此构造函数,插件的用途是什么,以及它在流的哪个状态下执行routeStartup、routeShutdown、predispatch等操作?