zf2设置导航栏的方法

时间:2012-07-18 23:14:49

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

我正在努力连接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);

我会在Module中执行此操作,还是在配置文件中更好地完成并注入?如果注入,怎么办?

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

2 个答案:

答案 0 :(得分:5)

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

在模块配置中添加此行,它将起作用。

答案 1 :(得分:1)

如何制作导航的简单示例

文件路径模块/ 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(); ?>