How can I load a Zend Framework plugin?
I have created a plugins directory in the application directory.
Currently I am loading the plugins like so:
protected function _initAccessCheck()
{
include('../application/plugins/AccessCheck.php');
$fc = 开发者_如何学GoZend_Controller_Front::getInstance();
$fc->registerPlugin( new Plugin_AccessCheck() );
}
What would I have to do so I would not have to use the include function? Many thanks in advance.
Zend_Loader_Autoloader_Resource
allows you to define a mapping between file paths and class names. This allows you to autoload classes whose files are not stored on the include path.
Typically, you would use the subclass Zend_Application_Module_Autoloader
which sets up some common mappings for models, forms, etc. In particular, it has an entry for plugins. In Bootstrap, it would be something like this:
protected function _initResourceLoader()
{
$resourceLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH,
));
return $resourceLoader;
}
Then a class named Application_Plugin_MyPlugin
would reside in the file application/plugins/MyPlugin.php
.
In your specific circumstance, it looks like you are using an empty namespace. So your's would be:
protected function _initResourceLoader()
{
$resourceLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH,
));
return $resourceLoader;
}
Then your plugin class Plugin_AccessCheck
would reside in the file application/plugins/AccessCheck.php
.
Just make sure that the resource loader is created before you instantiate/register your plugins:
protected function _initAccessCheck()
{
$this->booststrap('resourceLoader');
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin( new Plugin_AccessCheck() );
}
I use application.ini file to enable plugins, in the following way:
For example, for an authentication control plugin I have the following:
class Application_Plugin_AuthCheck extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
//check if user is logged in
}
}
And then I add it at application.ini
resources.frontController.plugins.authcheck = Application_Plugin_AuthCheck
And it's now registered at application.
Note: I use dispatchLoopStartup but you could use another function as specified here: http://framework.zend.com/manual/en/zend.controller.plugins.html
精彩评论