【问题标题】:zf2 way to set up a navigation barzf2方式设置导航栏
【发布时间】:2012-07-18 23:14:49
【问题描述】:

我正在努力将 ZF1 在引导程序中初始化事物的方式与 ZF2 从配置文件注入事物的方式(看似)联系起来。

也就是说,在 ZF1 中,我的 boostrap 中有这样的东西:

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

我应该在模块中执行此操作,还是在配置文件中执行并注入更好?如果注入,如何注入?

我已阅读 Rob Allen 的教程,并且一直在网上搜索超出教程级别代码的示例。我发现的东西(像其他 ZF2 模块一样)更倾向于作为工作模块(可以理解),而不是通过向其他人传达细微差别的示例......因为我在这个主题上找不到太多,我我假设我遗漏了一些小的、基本的东西——当我看到它时——这一切都是有意义的。

【问题讨论】:

    标签: zend-framework configuration navigation zend-framework2 initialization


    【解决方案1】:
    'service_manager' => array(
        'factories' => array(
            'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
        ),
    ),
    

    在您的模块配置中添加这一行,它将起作用。

    【讨论】:

      【解决方案2】:

      如何制作导航的简单示例

      文件路径模块/Application/config/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',
                          ),
                      ),
                  ),
              ),
          ),
      );
      

      最后回显导航布局文件或视图文件

      示例模块/Application/view/layout/layout.phtml

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

      【讨论】:

      • 它给了我这个异常:'Zend\ServiceManager\ServiceManager::get 无法获取或创建 Navigation 实例'
      猜你喜欢
      • 1970-01-01
      • 2012-10-09
      • 2020-05-13
      • 2016-12-11
      • 2011-09-03
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多