5

我在同一模块下配置多个命名空间/类时遇到问题。例如,我有一个名为“Account”的模块,我想在其中包含所有与帐户相关的类(公司:'accounts',用户:'users',外部 api:'api' 等)。模块结构看起来像这样..

        /Account
        - Module.php
        - /config
        - /view
        - /src
          - /Account
            - /Controller (AccountController.php)
            - /Form       (AccountForm.php)
            - /Model      (Account.php + AccountTable.php)
          - /User
            - /Controller (UserController.php)
            - /Form       (UserForm.php)
            - /Model      (User.php + UserTable.php)
          - /Api
            - Api.php     (simple class)

作为 ZF2 的新手,我决定让事情变得简单而愚蠢,而不是尝试实现到 Account 模块的复杂路由。因此,为了触发 UserController 的 indexAction,url 应该是 /user (!)

这是模块类:

namespace Account;

use Account\Model\AccountTable;
use Account\Model\UserTable;

class Module
{
    public function getAutoloaderConfig()
    {
        return array(
                            'Zend\Loader\ClassMapAutoloader' => array(
                                                                                        __DIR__ . '/autoload_classmap.php',
                                                                                  ),
                            'Zend\Loader\StandardAutoloader' => array(
                                                                                        'namespaces' => array(
                                                                                                                        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                                                                                                                    ),
                                                                               ),
        );
    }

    public function getServiceConfig()
    {
        return array(
                            'factories' => array(
                                                            'Account\Model\AccountTable'  =>  function($sm) {
                                                                                                                                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                                                                                                                                $table = new AccountTable($dbAdapter);
                                                                                                                                return $table;
                                                                                                                            },
                                                            'Account\Model\UserTable'           =>  function($sm) {
                                                                                                                                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                                                                                                                                $table = new UserTable($dbAdapter);
                                                                                                                                return $table;
                                                                                                                            },                                                              
                                                      ),
        );
    }    

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
}

和 module.config 文件

return array(
    'controllers' => array(
        'invokables' => array(
            'Account\Controller\Account'    => 'Account\Controller\AccountController',
            'Account\Controller\User'           => 'Account\Controller\UserController',
        ),
    ),

    'router' => array(
        'routes' => array(
            'account' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'      => '/account[/:action[/:accountId]]',
                    'constraints' => array(
                                                    'action'         => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                    'accountId'      => '[0-9]+',
                                                ),
                    'defaults' => array(
                        'controller' => 'Account\Controller\Account',
                        'action'     => 'index',
                    ),
                ),
/*
                'may_terminate' => true,
                'child_routes' => array(
                      'user' => array(
                            'type' => 'literal',
                            'options' => array(
                                'route' => '/user[/:action[/:userId]]',
                                'constraints' => array(
                                                    'action'         => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                    'userId'         => '[0-9]+',
                                                ),
                                'defaults' => array(
                                        'controller' => 'Account\Controller\User',
                                        'action'     => 'index'
                                )
                        )
                    )
                ),
*/
            ),
            'user' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'      => '/user[/:action[/:userId]]',
                    'constraints' => array(
                                                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                    'userId'     => '[0-9]+',
                                                ),
                    'defaults' => array(
                        'controller' => 'Account\Controller\User',
                        'action'     => 'index',
                    ),
                ),
             )


        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'account' => __DIR__ . '/../view',
            'user'    => __DIR__ . '/../view',

        ),
    ),
);

但我得到的错误是“找不到类'Account\Controller\UserController'”。我确定我错过了一些东西。请问有什么线索吗?

谢谢

4

2 回答 2

12

您必须StandardAutoloader告知您的新命名空间:

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

在 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',
        ),
    ),
    /* ... */
);
于 2013-04-03T05:23:03.610 回答
1

StandardLoader 需要知道在哪里可以找到类。您可以使用名为namespaces的选项来定义它,该选项是一个包含绝对(或相对于当前脚本)路径的数组。它应该如下所示:

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

__NAMESPACE__ 是模块的名称,而 __DIR__ 是 Module.php 脚本的绝对路径

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

ClassMapAutoloader 用于性能:您定义类键及其文件的确切路径,而不是 zf2 必须浏览其内容以执行文件系统操作的文件夹(StandardLoader 的方式)。

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

于 2013-04-03T04:41:45.017 回答