简体   繁体   中英

Zend Framework plugin autoloading

I have been trying to write a Controller Plugin which I will be using for user authentication. I have written the plugin and it should work but I just don't get how to get the plugin loaded... I have read that the Zend Framework has a lot of autoloading possibilities..

My current directory structure:

domains
example.com
    Application
        configs
        controllers
            IndexController.php
            AuthController.php
            ErrorController.php
        forms
        layouts
            scripts
                layout.phtml
        models
        plugins
            AuthenticationPlugin.php
        views
            helpers
            scripts
                auth
                    login.phtml
                error
                    error.phtml
                index
                    index.phtml
        Bootstrap.php
    library
        Zend
    pubic_html
        .htaccess
        index.php

Can anyone help me?

Thanks in advance!

Assuming your appnamespace is Application_ , then your plugin class should be:

  1. named Application_Plugin_AuthenticationPlugin

  2. stored in the file application/plugins/AuthenticationPlugin.php

  3. registered with the frontcontroller using something like (in application/configs/application.ini ):

    resources.frontController.plugins.auth = "Application_Plugin_AuthenticationPlugin"

You could create your own library folder with a similar folder structure to Zend's. For instance (assuming your own namespace My_ ):

library
  My
    Controller  
      Plugin
        Authentication.php

Authentication.php would contain a class named My_Controller_Plugin_Authentication .

You would then register the namespace in your bootstrap ( manual ):

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('My_');

Failing that, you could use the structure above using the resource autoloader ( manual ). Zend Framework expects that classes in those folders are namespace prefixed too, so your class name would be Plugin_AuthenticationPlugin .

在您的Bootstrap文件中添加以下行:

Zend_Controller_Front::getInstance()->registerPlugin(new AuthenticationPlugin());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM