为什么 Zend Framework 找不到我的控制器插件

发布于 2024-09-15 21:18:53 字数 1157 浏览 5 评论 0原文

我正在尝试编写控制器插件来检查身份验证。 我创建了插件类,放入Application目录中,Application.php并在Bootstrap.php中注册。但有一个错误:致命错误:找不到类“身份验证”。 Zend Framework 在哪里寻找插件,如何告诉它它在哪里?

//Application/Authentication.php
class Authentication extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            return;
        }

        self::setDispatched(false);
        // handle unauthorized request...
    }
}


        //bootstrap
    protected function _initAutoloader()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
        'basePath' => APPLICATION_PATH,
        'namespace' => ''));

        $autoLoader = Zend_Loader_Autoloader::getInstance();
        $autoLoader->registerNamespace('Common_');

        return $moduleLoader;              
    }



    protected function _initPlugins()
    {
        $controller = Zend_Controller_Front::getInstance();
        $controller->registerPlugin(new Authentication());
        $controller->dispatch();        
    }

谢谢。

I'm trying to write controller plugin to check authentication.
I created class of plugin, put in Application directory, Application.php and registered in Bootstrap.php. But there is an error: Fatal error: Class 'Authentication' not found.
Where does Zend Framework look for plugins, how to tell it where it is?

//Application/Authentication.php
class Authentication extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            return;
        }

        self::setDispatched(false);
        // handle unauthorized request...
    }
}


        //bootstrap
    protected function _initAutoloader()
    {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
        'basePath' => APPLICATION_PATH,
        'namespace' => ''));

        $autoLoader = Zend_Loader_Autoloader::getInstance();
        $autoLoader->registerNamespace('Common_');

        return $moduleLoader;              
    }



    protected function _initPlugins()
    {
        $controller = Zend_Controller_Front::getInstance();
        $controller->registerPlugin(new Authentication());
        $controller->dispatch();        
    }

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2024-09-22 21:18:53

我知道这个问题确实很老,但我会留下答案,以防其他人像我一样偶然发现这里。以下是(从版本 1 到 1.8 及更高版本)如何注册插件:

ZF 遵循命名标准: A_B 解析为 A/B.php 。对于插件,ZF 会自动查找“库路径”,这意味着它会查找库的目录(Zend 库所在的位置)。所以插件应该如下:library/Something/Whatever.php ...这是一种情况。然后您所要做的就是在 application.ini 中添加以下内容:

autoloaderNamespaces[] = "Something_"
resources.frontController.plugins.Whatever = "Something_Whatever"

翻译成您的情况将是:

autoloaderNamespaces[] = "Common_"
resources.frontController.plugins.Authentication = "Common_Authentication"

您的库结构应该是:

library/Common/Authentication.php

希望这对任何在这里绊倒的人有所帮助!

--关于您的帖子/问题

之所以没有“找到”该类,是因为它没有使用自动加载来加载。原因之一可能是您在某种程度上违反了命名约定(您的 Authentication 文件不在目录 Common_ 下,或者 Authentication 类的文件名不是 Common_Authentication ...)。一个快速修复方法是:

//bootstrap
    protected function _initAutoloader()
    {
        require_once 'Common/Authentication.php';           
    }

通过此添加,_initPlugins() 将能够毫无问题地执行。 :)

I know the question is really old, but I´m leaving the answer in case someone else stumbles upon here like I did. Here is (from version 1 from 1.8 and up) how to register a plugin:

ZF follows the naming standard: A_B parses to A/B.php . For the plugin, ZF automatically looks into the "path to library", which means that it looks within the directory of your library (where your Zend library is). So the plugin should be as follows: library/Something/Whatever.php ... That´s one scenario. Then all you have to do in application.ini is add the following:

autoloaderNamespaces[] = "Something_"
resources.frontController.plugins.Whatever = "Something_Whatever"

Translated to your case would be:

autoloaderNamespaces[] = "Common_"
resources.frontController.plugins.Authentication = "Common_Authentication"

And your library structure should be:

library/Common/Authentication.php

hope this helps to anyone stumbling upon here!

--Regarding your post/question

The reason why it´s not "finding" the class it´s because it´s not loading with autoload. One reason might be that you're somehow violating the naming convention (Your Authentication file is not under directory Common_ , or the file name of Authentication class is not Common_Authentication ...). A quick fix would be to put:

//bootstrap
    protected function _initAutoloader()
    {
        require_once 'Common/Authentication.php';           
    }

with this addes, _initPlugins() will be able to execute without problem. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文