Multiple namespaces under same module in ZF2

前端 未结 2 2014
滥情空心
滥情空心 2021-01-05 18:04

I\'m having trouble configuring multiple namespaces/classes under same module. For example, I have a module called \"Account\", in which I\'d like to include all account rel

相关标签:
2条回答
  • 2021-01-05 18:53

    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',
            ),
        ),
        /* ... */
    );
    
    0 讨论(0)
  • 2021-01-05 18:57

    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

    0 讨论(0)
提交回复
热议问题