2

我正在努力将 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 模块一样)更倾向于作为工作模块(可以理解),而不是通过将细微差别传达给其他人的示例......因为我在这个主题上找不到太多,我我假设我遗漏了一些小的、基本的东西——当我看到它时——这一切都会变得有意义。

4

2 回答 2

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

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

于 2012-10-13T22:46:36.717 回答
1

如何制作导航的简单示例

文件路径 module/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(); ?>
于 2012-08-05T15:49:29.110 回答