2013-04-03 10 views
5

Aynı modül altında birden fazla ad alanı/sınıf yapılandırmak konusunda sorun yaşıyorum. Örneğin, tüm hesapla ilgili sınıfları dahil etmek istediğim "Hesap" adlı bir modülüm var (şirketler: 'hesaplar', kullanıcılar: 'kullanıcılar', harici API: 'api' vb ..). Modül yapısı aşağıdaki gibidir ..ZF2'de aynı modülün altında birden çok ad alanı

 /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) 

yeni olmak ZF2, ben basit ve aptalca şeyler tutmaya karar verdi ve modül Hesap karmaşık yönlendirme uygulanması zorlamak istemem. Yani, UserController için indexAction tetiklemek için, url/kullanıcı olmalıdır İşte

modül sınıf var (!):

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

Ve module.config dosyası

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

     ), 
    ), 
); 

Ama hata Ben alıyorum, "Sınıf 'Hesap \ Controller \ UserController' bulunamadı". Eminim bir şey özledim. Herhangi bir ipucu lütfen?

Teşekkür

cevap

11

Sen StandardAutoloader yeni ad bildirmek gerekir:

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 olarak Cevabınız için

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

Çok teşekkürler dostum! Özel ad alanlarının yüklenmediğini fark etmediniz. –

1

StandardLoader nerede sınıfları bulmak için bilmesi gerekir. ad alanları adında bir seçenekle tanımlayabilirsiniz; bu, mutlak (veya geçerli komut dosyasına göre) yollarını içeren bir dizidir. Bu gibi görünmelidir:

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

__NAMESPACE__ modülünün adıdır ve __DIR__ mutlak yolunu Module.php senaryoya

ClassMapAutoloader performansı için kullanılır Kontrol http://framework.zend.com/manual/2.0/en/modules/zend.loader.standard-autoloader.html

: Eğer zf2'nin dosya sistemi işlemlerini yaparak içeriğine göz atması gereken bir klasör yerine, sınıf anahtarını ve dosyanın tam yolunu tanımlar (StandardLoader'ın yolu).

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

+0

çok teşekkür ederiz. Ama ben zaten names.acs (getAutoloaderConfig()). Demek istediğin bu mu? –

+0

Ups! Üzgünüm yatay kaydırma kullanmamıştım ve dizilerinizin boş olduğunu düşündüm. Eğer kaydırma yapmazsanız parantez doğru kapanır! Sorunu çözmene sevindim. – lluisaznar

İlgili konular