2013-04-03 9 views
5

Ich habe Probleme beim Konfigurieren mehrerer Namespaces/Klassen unter demselben Modul. Zum Beispiel habe ich ein Modul namens "Konto", in dem ich alle kontobezogenen Klassen (Unternehmen: "Konten", Benutzer: "Benutzer", externe API: "API" etc ..) einschließen möchte. Modulstruktur sieht wie folgt aus ..Mehrere Namespaces unter demselben Modul in 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) 

Sein neues, ZF2, ich beschlossen, die Dinge einfach zu halten und dumm und nicht komplexe Routing-Implementierung zu versuchen Modul zu berücksichtigen. Also, um index für Usercontroller auslösen sollte url/user sein

Hier ist die Modulklasse (!):

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

Und die module.config Datei

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

     ), 
    ), 
); 

Aber der Fehler Ich bekomme ist, "Class 'Account \ Controller \ UserController' nicht gefunden". Ich bin mir sicher, dass ich etwas verpasst habe. Irgendwelche Hinweise bitte?

Dank

Antwort

11

Sie müssen die StandardAutoloader über Ihren neuen Namensraum wissen lassen:

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

Im 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

Vielen Dank Kumpel! Es wurde nicht erkannt, dass benutzerdefinierte Namespaces nicht geladen wurden. –

1

Das muss StandardLoader wissen, wo die Klassen zu finden. Sie können es mit einer Option namens Namespaces definieren, die ein Array ist, das absolute (oder relativ zum aktuellen Skript) Pfade enthält. Es sollte wie folgt aussehen:

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

__NAMESPACE__ ist der Name des Moduls, und __DIR__ den absoluten Pfad zum module.php Skript

prüfen http://framework.zend.com/manual/2.0/en/modules/zend.loader.standard-autoloader.html

Die ClassMapAutoloader für die Leistung verwendet wird: Sie Definieren Sie den Klassenschlüssel und seinen genauen Pfad zur Datei, anstelle eines Ordners, in dem zf2 seinen Inhalt durchsuchen muss, indem er Dateisystemoperationen ausführt (StandardLoader).

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

+0

Vielen Dank für Ihre Antwort. Aber ich lade bereits Namespaces in module.php (getAutoloaderConfig()). Meinst Du das? –

+0

Ups! Tut mir leid, dass ich den horizontalen Bildlauf nicht verwendet habe und ich dachte, Ihre Arrays wären leer. Wenn Sie nicht blättern, schließt die Klammer rechts! Freut mich, dass du dein Problem löst. – lluisaznar

Verwandte Themen