Multiple namespaces under same module in ZF2

℡╲_俬逩灬. 提交于 2019-11-30 21:27:52

You must let the StandardAutoloader know about your new namespace:

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                // This is for the Account namespace
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                // And this is for the User namespace
                'User'        => __DIR__ . '/src/' . 'User',
            ),
        ),
    );
}

In the module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Account\Controller\Account' => 'Account\Controller\AccountController',
            // The key can be what ever you want, but the value must be a valid
            // class name. Your UserController lives in the User namespace,
            // not in Account
            'Account\Controller\User'    => 'User\Controller\UserController',
        ),
    ),
    /* ... */
);

The StandardLoader needs to know where to find the classes. You can define it with an option called namespaces which is an array that contains absolute (or relative to the current script) paths. It should look like this:

'Zend\Loader\StandardAutoloader' => array(
    'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
    ) 
) 

__NAMESPACE__ is the name of the module, and __DIR__ the absolute path to the Module.php script

Check http://framework.zend.com/manual/2.0/en/modules/zend.loader.standard-autoloader.html

The ClassMapAutoloader is used for performance: you define the class key and its exactly path to the file, instead of a folder which zf2 has to browse its contents doing filesystem operations (StandardLoader's way).

Check http://framework.zend.com/manual/2.0/en/modules/zend.loader.class-map-autoloader.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!