Zend 资源自动加载器不适用于命名空间 [英] Zend Resource Autoloaders not working for namespaces

查看:27
本文介绍了Zend 资源自动加载器不适用于命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Zend 框架时遇到了自动加载问题.基本上在库文件夹中有一个名为 LunaZend 的文件夹.LunaZend 有一些可以在 Zend Framework 中使用的类,这些类有命名空间,只能通过调用命名空间名称来自动加载.命名空间就像 LunaZend\DB, LunaZend\Etc ...在引导程序中,我有一个 _initAutoLoadNS 函数,它具有

I have this autloading struggle with the Zend Framework. Basically there is a folder named LunaZend in library folder. LunaZend has some classes which can be used in Zend Framework and these classes have namespaces and must be loaded automatically only by calling namespace names. Namespaces are like LunaZend\DB, LunaZend\Etc ... In bootstrap I have an _initAutoLoadNS function which has

$resource = new Zend_Loader_Autoloader_Resource(array(
                        'basePath'  =>  APPLICATION_PATH.'/../library/LunaZend/',
                        'namespace' => 'LunaZend')
       );

我希望能够仅通过调用 like 来加载 Zend Framework 中的类

I want to be able to load classes in Zend Framework only by calling like

$t = new LunaZend\Di\DependencyInjector();

但是我得到了错误

致命错误:类未找到LunaZend\Di\DependencyInjector"在...我究竟做错了什么?如何处理这个命名空间自动加载问题?

Fatal error: Class 'LunaZend\Di\DependencyInjector' not found in... What am I doing wrong? How to deal with this namespace autoloading issue?

谢谢.

推荐答案

是的,这个问题似乎偶尔会出现,直到掌握了 ZF 中的命名空间(本机命名空间).这是我对此的看法.这适用于所有只想正确加载一些使用命名空间的第三方的人.太简单了.

Yeah, well, this question seems to pop-up once in a while until a got the hang of namespaces (native ones) in ZF. Here's my take on this. This is for all you out there who just want to proper load some third party that's using namespaces. It's dead simple.

我正在使用 ZF 1.11.11(根据文档,所有 ZF 版本 1.10+ 都可以工作).

I'm using ZF 1.11.11 (as per documentation, all ZF versions 1.10+ work).

首先,从 1.10 开始,ZF 支持原生 PHP 命名空间自动加载,前提是它们符合 PSR-0 标准.

First of all, as of 1.10, ZF supports native PHP namespaces autoloading, provided they conform to the PSR-0 standard.

我想在 ZF1 项目中添加 Symfony2 EventManager 组件.
首先,与类名一样,命名空间必须与库中的路径匹配.因此,命名空间 Symfony\Component\EventDispatcher\EventDispatcher 映射到 path/to/lib/Symfony/Component/EventDispatcher/EventDispatcher.php(其中 path/to/lib/APPLICATION_PATH .'/library';你懂的).我必须提到库文件夹必须在 include_path 中吗?不,我想我没有.

I wanted to add the Symfony2 EventManager component in a ZF1 project.
First off all, like with the class names, the namespace must match with the path in library. So, the namespace Symfony\Component\EventDispatcher\EventDispatcher maps to path/to/lib/Symfony/Component/EventDispatcher/EventDispatcher.php (where path/to/lib/ is APPLICATION_PATH . '/library'; you get the idea). Must I mention that the library folder must be in include_path? No, I guess I don't.

现在有了不那么棘手的部分:

Now with the-not-so-tricky-part:

<?php

// bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initNsAutoload()
    {
        $loader = Zend_Loader_Autoloader::getInstance();
        $loader->registerNamespace('Symfony'); // this is why is dead simple

        /* Don't believe me? Try it:

        Zend_Registry::set('events', new Symfony\Component\EventDispatcher\EventDispatcher());

        */
    }
}

如何在控制器中使用命名空间导入?

How about using namespace imports in controllers?

<?php

use Symfony\Component\EventDispatcher\Event, 
    Symfony\Component\EventDispatcher\EventDispatcher;

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        var_dump(new EventDispatcher());
        var_dump(new Event());
    }
}

因此,只要您使用的是 ZF 1.10+,就不需要自定义自动加载器.这个回复是在我偷看 这个.

So, as long as you're on ZF 1.10+, there's no need for a custom autoloader. This reply was made after I peeked at this.

LE:或将其添加到 application.ini:
autoloaderNamespaces[] = Symfony

LE: or add this to application.ini:
autoloaderNamespaces[] = Symfony

这篇关于Zend 资源自动加载器不适用于命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆