Zend Resource Autoloaders not working for namespac

2020-03-24 07:45发布

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')
       );

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

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

However I get the error

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

Thank you.

3条回答
疯言疯语
2楼-- · 2020-03-24 08:19

You can use application.ini to load some namespace.

I used the same

Following can be application.ini' sample code:-

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
appnamespace = "Application"

;Autoloader Namespace
autoloaderNamespaces.w = "LunaZend_"
查看更多
在下西门庆
3楼-- · 2020-03-24 08:23

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.

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

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

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());
    }
}

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: or add this to application.ini:
autoloaderNamespaces[] = Symfony

查看更多
放我归山
4楼-- · 2020-03-24 08:24

AFAIK, the ZF1 autoloader does not handle genuine PHP 5.3 namespaced classes. To use the ZF1 autoloader on true namespaced classes, you need to configure the separator variable to be \. (Thanks to @Mattieu for the correction). But Zend_Loader_Autoloader_Resource doesn't handle the path mapping the way we might expect.

You could use a namespace-aware autoloader, like that of Doctrine2 or ZF2. Pushing one of these autoloaders onto the standard ZF1 autoloader stack should handle it.

查看更多
登录 后发表回答