2

我很努力地在引导程序中初始化事物的ZF1方法和从配置文件(看似)注入事物的ZF2方法之间建立连接点。zf2设置导航栏的方法

要机智,在ZF1,我在我的自举这样的事情:

protected function _initNavigation() 
{ 
    $this->bootstrap('layout'); 
    $this->bootstrap('view'); 

    $navigation = new Zend_Navigation(); 

    // ...code to add pages... 

    $layout = $this->getResource('layout'); 
    $view = $layout->getView(); 

    $view->navigation($navigation); 
} 

在ZF2,我甚至不能确定开始寻找,完成类似的东西是什么。

我读过参考职位:

public function onBootstrap (Event $e) 
{ 
} 

而且,你可以做这样的事情的方式:

$application = $e->getApplication(); 
$services = $application->getServiceManager(); 

但是,什么是等价的:

$layout = $this->getResource('layout'); 
$view = $layout->getView(); 
$view->navigation($navigation); 

我会在Module中做这个,还是在配置文件中做得更好,然后注入?如果注入,如何?

我读过罗布艾伦的教程,并且一直在寻找超出教程级代码范例的网络。我发现的东西(与其他ZF2模块一样)已经更多地面向工作模块(可以理解),而不是通过向别人传达细微差别的例子......因为我无法在这个主题上找到太多东西,所以我假设我错过了一些小的,基本的东西 - 当我看到它时 - 将会有所帮助。

回答

1

简单的例子HOWTO使导航

文件路径模块/应用/配置/ module.config.php

<?php 
return array(
    'router' => array(
     'routes' => array(
      'home' => array(
       'type' => 'Zend\Mvc\Router\Http\Segment', 
       'options' => array(
        'route' => '/', 
        'defaults' => array(
         'controller' => 'Application\Controller\Index', 
         'action' => 'index', 
        ), 
       ), 
      ), 
      'default' => array(
       'type' => 'Zend\Mvc\Router\Http\Segment', 
       'options' => array(
        'route' => '/[:namespace[/:controller[/:action]]]', 
        'constraints' => array(
         'namespace' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
        ), 
        'defaults' => array(
         //'locale' => 'da_DK', 
         'namespace' => 'Application', 
         'controller' => 'index', 
         'action' => 'index', 
        ), 
       ), 
      ), 
     ), 
    ), 
    'controllers' => array(
     'invokables' => array(
      'index' => 'Application\Controller\IndexController', 
      'Application\Controller\Index' => 'Application\Controller\IndexController' 
     ), 
    ), 
    'service_manager' => array(
     'factories' => array(
      'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
     ), 
    ), 
    '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', 
      'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 
      'error/404'    => __DIR__ . '/../view/error/404.phtml', 
      'error/index'    => __DIR__ . '/../view/error/index.phtml', 
     ), 
     'template_path_stack' => array(
      __DIR__ . '/../view', 
     ), 
    ), 
); 

接下来是导航配置

<?php 
/* 
* This file path config/autoload/application.global.php 
*/ 
return array(
    // All navigation-related configuration is collected in the 'navigation' key 
    'navigation' => array(
     // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key 
     'default' => array(
      // And finally, here is where we define our page hierarchy 
      'home' => array(
       'label' => 'Home', 
       'route' => 'home', 
      ), 
      'news' => array(
       'label' => 'News', 
       'controller' => 'news', 
       'action' => 'news', 
       'route' => 'default', 
       'pages' => array(
        'add' => array(
         'label' => 'Add news', 
         'controller' => 'news', /* or create a seperate route insteed*/ 
         'action' => 'add', 
         'route' => 'default', 
        ), 
       ), 
      ), 
     ), 
    ), 
); 

而去年回波导航布局文件或查看文件

例如模块/应用/视图/布局/ layout.phtml

<?php echo $this->navigation('Navigation')->menu(); ?> 
+1

它给了我这样的例外:“Zend的\的ServiceManager \的ServiceManager ::得到无法获取或创建导航实例” @Behzadsh我 – Behzadsh 2012-08-25 13:34:10

+0

了。 – ioanb7 2012-11-08 12:39:32

5
'service_manager' => array(
    'factories' => array(
     'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
    ), 
), 

加入了这一行你的模块的配置,它会工作。