【问题标题】:Zend Framework plugin autoloadingZend Framework 插件自动加载
【发布时间】:2012-03-07 21:17:10
【问题描述】:

我一直在尝试编写一个控制器插件,我将使用它来进行用户身份验证。 我已经编写了插件,它应该可以工作,但我只是不知道如何加载插件...我读过 Zend 框架有很多自动加载的可能性..

我当前的目录结构:

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

谁能帮帮我?

提前致谢!

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    假设你的appnamespace 是Application_,那么你的插件类应该是:

    1. 命名为Application_Plugin_AuthenticationPlugin

    2. 存储在文件application/plugins/AuthenticationPlugin.php

    3. 使用类似(application/configs/application.ini)之类的方式向前端控制器注册:

      resources.frontController.plugins.auth = "Application_Plugin_AuthenticationPlugin"

    【讨论】:

      【解决方案2】:

      您可以创建自己的库文件夹,其文件夹结构与 Zend 的类似。例如(假设你自己的命名空间My_):

      library
        My
          Controller  
            Plugin
              Authentication.php
      

      Authentication.php 将包含一个名为 My_Controller_Plugin_Authentication 的类。

      然后,您将在引导程序中注册命名空间 (manual):

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

      如果做不到这一点,您可以使用资源自动加载器 (manual) 来使用上述结构。 Zend Framework 期望这些文件夹中的类也有命名空间前缀,所以你的类名应该是Plugin_AuthenticationPlugin。

      【讨论】:

        【解决方案3】:

        例如在您的引导文件中添加以下行:

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

        【讨论】:

        • 然后我收到消息说找不到 AuthenticationPlugin 类...我可以在 Library 目录中创建另一个目录并在那里使用插件吗?
        • 你的目录结构没问题。在 AuthenticationPlugin.php 中,尝试将您的类重命名为 Zend_Plugin_AuthenticationPlugin,并将上面的行更改为 Zend_Controller_Front::getInstance()->registerPlugin(new Zend_Plugin_AuthenticationPlugin());
        • 我建议不要将自定义 app/lib 类放在 Zend_ 命名空间中。我认为最好在 appnamespace 或单独的 lib 命名空间中命名它们。
        猜你喜欢
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多