What directory is used for Zend plugins?
Let's say I have the following in my ini file:
resources.frontController.plugins.auth = AuthPlugin
Where should the AuthPlugin class be placed? Let's say I would like it under controllers/plugins.
UPDATE:
Based on the suggestions below I am still having trouble. Let me be exact in what I currently have:
1) main part of application.ini
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[] =
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.plugins.authplugin.class = "AuthPlugin"
2) my Bootstrap.php has nothing (I had lots of things in there, but still get the error with nothing):
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
3) I have an AuthPlugin.php class in application/plugins directory
class AuthPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
// code here
}
}
I get the following error:
Fatal error: Class 'AuthPlugin' not found in C:\[my dir structure he开发者_如何学编程re]\Application\Resource\Frontcontroller.php on line 111
I assume I'm missing something obvious here. Thanks in advance. Zend Framework 1.10
This is how I register a plugin named Foo_Plugin_SuperDuperPlugin in my application config:
resources.frontController.plugins.superduperplugin.class = "Foo_Plugin_SuperDuperPlugin"
The plugin is located at
APPLICATION_PATH/plugins/Foo_Plugin_SuperDuperPlugin.php
and is autoloaded from there because the Resource Module Autoloader automatically looks in that (recommended) location for plugin type resources. If I wanted to load the plugin from, say,
APPLICATION_PATH/controllers/plugins/Foo_Plugin_SuperDuperPlugin.php
then I would register a new resource loader with the autoloader and define a type of resource named 'plugin' and the path to those plugin resources. So in my bootstrap.php
protected function _initAutoloader()
{
$autoloader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Foo',
'resourceTypes' => array(
'plugin' => array(
'path' => 'controllers/plugins',
'namespace' => 'Plugin',
)
)
)
);
}
and then I need to ensure that this method is bootstrapped before the SuperDuperPlugin is registered (which, in this example, happens when the application config is read resources.frontcontroller.plugins.superduperplugin.class = ...
). This can be achieved by placing the _initAutoloader
method at the top of the bootstrap.php or by calling $this->bootstrap('autoLoader');
from any other _init method, before the frontController resource is initialised.
UPDATED: Try adding this to your bootstrap:
protected function _initAutoloader()
{
$autoloader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'resourceTypes' => array(
'plugin' => array(
'path' => 'controllers/plugins',
'namespace' => '',
)
)
)
);
}
and maybe even leave off the namespace. Or: add appnamespace = "Foo"
to your config and rename the class to Foo_Plugin_AuthPlugin
.
hmm am having the exact same issue in zf 1.11. It seems the autoloader does not exists before the plugins get loaded :s
I think in /application/plugins/
But you could also set another Directory for it.
Since the application will bootstrap itself from the config file after registering the Autoloader, you should put AuthPlugin.php (which should contain the AuthPlugin class) in the include path.
I realise this is an old question, but I've just been searching around for ages for a solution to autoloading plugins outside of the library and have finally figured it out. Generally, I have been writing generic plugins to use across many different projects and keeping them in the library made sense, providing you follow the standard ZF naming conventions these will autoload anyway. Recently, I was writing a project specific plugin and I wanted to keep it somewhere within my application directory, but how to autoload? I've never found that using the "application/plugins" directory worked. Hopefully this might benefit someone else:
If you are using a modular directory structure then rather than using the "application/plugins" directory you can use "application/module_name/plugins". You can then take advantage of the Module Resource Autoloader, which is a massively useful and underused part of ZF. If you set it up in your bootstrap, by default it is set up to autoload a whole bunch of stuff including forms, models and (I discovered today) plugins. You can also define your own custom resource types. For example below is an _initAutoloader function from a bootstrap in a project which has a default and admin module and a custom 'vo' resource type contained in the admin module:
public function _initAutoLoader() {
$autoloader = Zend_Loader_Autoloader::getInstance();
$defaultLoader = new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules/default'
)
);
$adminLoader = new Zend_Application_Module_Autoloader(
array(
'namespace' => 'Admin',
'basePath' => APPLICATION_PATH . '/modules/admin',
)
);
$adminLoader->addResourceTypes(
array(
'vo' => array(
'path' => 'models/vo',
'namespace' => 'Vo',
)
)
);
$autoloader->pushAutoloader($defaultLoader);
$autoloader->pushAutoloader($adminLoader);
}
Here I have two modules, default and admin. Assuming the following directory structure:
application/default/forms/FooForm.php
application/default/models/FooModel.php
application/default/plugins/FooPlugin.php
application/admin/forms/FooForm.php
application/admin/models/FooModel.php
application/admin/models/vo/FooVo.php
application/admin/plugins/FooPlugin.php
I can autoload across any module just by instantiating an object of each class:
new Form_FooForm();
new Model_FooModel();
new Plugin_FooPlugin(); // or add plugin to application.ini
new Admin_Form_FooForm();
new Admin_Model_FooModel();
new Admin_Vo_FooVo(); // custom resource type
new Admin_Plugin_FooPlugin(); // or add plugin to application.ini
精彩评论