2013-01-31 193 views
3

我是Zend Framework 2的新手。我幾天前開始使用它。過去三天我一直在爲模塊結構而努力。我想有2個模塊:主要和管理員。Zend Framework 2模塊

我有application.config.php文件: 「管理員」 模塊的

return array(
    'modules' => array(
     'main', 'Administrator', 
    ), 
    'module_listener_options' => array(
     'module_paths' => array(
      './module', 
      './vendor', 
     ), 
     'config_glob_paths' => array(
      'config/autoload/{,*.}{global,local}.php', 
     ), 
    ), 
); 

module.config.php:

return array(
    'router' => array(
     'routes' => array(
      'Administrator' => array(
       'type' => 'Literal', 
       'options' => array(
        'route' => '/administrator', 
        'defaults' => array(
         '__NAMESPACE__' => 'Administrator\Controller', 
         'controller' => 'Index', 
         'action' => 'index', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'default' => array(
         'type' => 'Segment', 
         'options' => array(
          'route' => '/[:controller[/:action]]', 
          'constraints' => array(
           'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
           'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
          ), 
          'defaults' => array(
          ), 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
    'service_manager' => array(
     'factories' => array(
      'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', 
     ), 
    ), 
    'controllers' => array(
     'invokables' => array(
      'Administrator\Controller\Index' => 'Administrator\Controller\IndexController', 
      'Administrator\Controller\Blogs' => 'Administrator\Controller\BlogsController', 
      'Administrator\Controller\Design' => 'Administrator\Controller\DesignController', 
      'Administrator\Controller\FAQ' => 'Administrator\Controller\FAQController', 
      'Administrator\Controller\Interests' => 'Administrator\Controller\InterestsController', 
      'Administrator\Controller\Main' => 'Administrator\Controller\MainController', 
      'Administrator\Controller\Pages' => 'Administrator\Controller\PagesController', 
      'Administrator\Controller\RSS' => 'Administrator\Controller\RSSController', 
      'Administrator\Controller\Users' => 'Administrator\Controller\UsersController' 
     ), 
    ), 
    'view_manager' => array(
     'display_not_found_reason' => true, 
     'display_exceptions' => true, 
     'doctype' => 'HTML5', 
     'not_found_template' => 'error/404', 
     'exception_template' => 'error/index', 
     'template_map' => array(
      'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 
      'administ/index/index' => __DIR__ . '/../view/administrator/index/index.phtml', 
      'error/404' => __DIR__ . '/../view/error/404.phtml', 
      'error/index' => __DIR__ . '/../view/error/index.phtml', 
     ), 
     'template_path_stack' => array(
      __DIR__ . '/../view', 
     ), 
    ), 
); 

和module.config.php的 「主」模塊:

return array(
    'router' => array(
     'routes' => array(
      'home' => array(
       'type' => 'Literal', 
       'options' => array(
        'route' => '/', 
        'defaults' => array(
         '__NAMESPACE__' => 'Main\Controller', 
         'controller' => 'Index', 
         'action' => 'index', 
        ), 
       ), 
      ), 
      'Main' => array(
       'type' => 'Literal', 
       'options' => array(
        'route' => '/main', 
        'defaults' => array(
         '__NAMESPACE__' => 'Main\Controller', 
         'controller' => 'Index', 
         'action' => 'index', 
        ), 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'default' => array(
         'type' => 'Segment', 
         'options' => array(
          'route' => '/[:controller[/:action]]', 
          'constraints' => array(
           'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
           'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
          ), 
          'defaults' => array(
          ), 
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
    'service_manager' => array(
     'factories' => array(
      'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', 
     ), 
    ), 
    'controllers' => array(
     'invokables' => array(
      'Main\Controller\Index' => 'Main\Controller\IndexController' 
     ), 
    ), 
    'view_manager' => array(
     'display_not_found_reason' => true, 
     'display_exceptions' => true, 
     'doctype' => 'HTML5', 
     'not_found_template' => 'error/404', 
     'exception_template' => 'error/index', 
     'template_map' => array(
      'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 
      'main/index/index' => __DIR__ . '/../view/main/index/index.phtml', 
      'error/404' => __DIR__ . '/../view/error/404.phtml', 
      'error/index' => __DIR__ . '/../view/error/index.phtml', 
     ), 
     'template_path_stack' => array(
      __DIR__ . '/../view', 
     ), 
    ), 
); 

這是我的文件結構: structure

我編輯這篇文章有點。現在看來,問題現在只有佈局。無論我打開什麼,它都會顯示「管理員」模塊的佈局和正確的頁面內容(或錯誤消息,取決於控制器/模塊/操作是否存在)。所以這個問題似乎只與佈局有關。

P.S.當我在application.config.php首先列出管理員模塊:

'modules' => array(
    'Administrator', 'Main' 
), 

它僅示出了「主」模塊佈局,反之亦然 - 當「主」是模塊陣列中的第一個元素 - 管理員佈局無處不在。

+0

https://github.com/EvanDotPro/EdpModuleLayouts – Xerkus

+0

爲模塊命名慣例是使用你的前綴和名字組合。例如EdpModuleLayouts:Edp是前綴,ModuleLayouts是名稱 – Xerkus

回答

1
'view_manager' => array(
    'display_not_found_reason' => true, 
    'display_exceptions' => true, 
    'doctype' => 'HTML5', 
    'not_found_template' => 'error/404', 
    'exception_template' => 'error/index', 
    'template_map' => array(

     // The following key will be overriden, and the last loaded module config 
     // is the one used, just comment it (for both modules config) the default 
     // behavior will pick-up the default/conventional layout. 

     //'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 

     'administ/index/index' => __DIR__ . '/../view/administrator/index/index.phtml', 
     'error/404' => __DIR__ . '/../view/error/404.phtml', 
     'error/index' => __DIR__ . '/../view/error/index.phtml', 
    ), 
    'template_path_stack' => array(
     __DIR__ . '/../view', 
    ), 
), 
+0

那麼,我該如何使用模塊特定的佈局? –

+0

我試圖在config/autoload/global.php中添加module_layouts數組,並在模塊特定配置中評論設置,但目前爲止尚未成功。 –

+0

確保在Composer配置文件中添加EdpModule依賴項,運行'php composer.phar update',並在你的配置文件中啓用該模塊。 請參閱:https://github.com/EvanDotPro/EdpModuleLayouts – yechabbi