2013-04-03 30 views
5

我在配置同一模塊下的多個名稱空間/類時遇到了問題。 例如,我有一個名爲「Account」的模塊,其中我想包括所有與賬戶相關的類(公司:'accounts',users:'users',external api:'api'等)。模塊結構看起來像這樣..ZF2中同一模塊下的多個名稱空間

 /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,我決定讓事情變得簡單和愚蠢的,而不是試圖實現複雜的路由到帳戶模塊。因此,爲了引發的indexAction爲UserController中,URL應該是/用戶

這裏的模塊類(!):

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', 

     ), 
    ), 
); 

但錯誤我得到的是「Class'Account \ Controller \ UserController'找不到」。我相信我錯過了一些東西。有任何線索嗎?

謝謝

回答

11

你必須讓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', 
     ), 
    ), 
    /* ... */ 
); 
+0

非常感謝隊友!沒有意識到自定義名稱空間沒有加載。 –

1

StandardLoader需要知道在哪裏可以找到類。您可以使用名爲命名空間的選項來定義它,該選項是包含絕對路徑(或相對於當前腳本)的路徑的數組。它應該是這樣的:

'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

+0

你的答案非常感謝。但是我已經在module.php(getAutoloaderConfig())中加載了命名空間。你是這個意思嗎? –

+0

Ups!對不起,我沒有使用水平滾動,我認爲你的數組是空的。如果你不滾動括號,就會關閉!很高興你解決你的問題。 – lluisaznar

相關問題