【问题标题】:Multiple namespaces under same module in ZF2ZF2中同一模块下的多个命名空间
【发布时间】:2013-03-24 12:23:42
【问题描述】:

我在同一模块下配置多个命名空间/类时遇到问题。 例如,我有一个名为“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'”。我确定我错过了一些东西。请问有什么线索吗?

谢谢

【问题讨论】:

    标签: php module namespaces zend-framework2


    【解决方案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

    【讨论】:

    • 非常感谢您的回答。但是我已经在 module.php (getAutoloaderConfig()) 中加载了命名空间。是这个意思吗?
    • 起来!对不起,我没有使用水平滚动,我认为你的数组是空的。如果你不滚动括号右关闭!很高兴你解决了你的问题。
    【解决方案2】:

    您必须让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',
            ),
        ),
        /* ... */
    );
    

    【讨论】:

    • 非常感谢伙计!没有意识到自定义命名空间没有加载。
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2013-11-04
    • 1970-01-01
    • 2020-07-14
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多