Zend 框架 2:Zend_Navigation [英] Zend Framework 2: Zend_Navigation

查看:28
本文介绍了Zend 框架 2:Zend_Navigation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道在 Zend Framework2 中使用 Zend_Navigation 的最佳方法是什么.目前我正在尝试为页眉和页脚菜单添加配置.两个菜单都应该自动填充.不过,我遇到了很多问题:

I am just wondering what would be the best way, to use Zend_Navigation in Zend Framework2. At the moment i am trying to a add a Config for a Header and a Footer Menu. Both Menus should be populated automatically. Though, i have run into a lot of Problems:

a) 默认菜单的最佳位置应该是应用程序模块

a) The best place for the Default Menu should be hopefully the Application-Module

b) 我将以下内容添加到 module.config.php 的根目录中:

b) I added the following to the root of module.config.php:

'view_helpers' => array(
    'helper_map' => array(
            'navigation' => 'Application\View\Helper\Navigation'
    ),
),

我的 Navigation.php 位于 ./module/Application/src/Application/View/Helper/Navigation.php

My Navigation.php is located in ./module/Application/src/Application/View/Helper/Navigation.php

并包含:

class Navigation extends AbstractContainer {
    public function __construct($menu = null) {
        var_dump($menu);
    }
}

我知道班级可能是错的.但此刻我什至没有让 ZF2 加载类.怎么了?

I know that the Class may be wrong. But at the Moment i did not get ZF2 to even load the Class. What is wrong ?

推荐答案

假设您使用的是 beta5 - 您只是错误地配置了视图助手.请参阅这篇文章,了解如何正确地做.

Assuming you're using beta5 - you're just configuring your viewhelper wrong. See this post on how to do it correctly.

关于如何使用导航:我会在 servicemanager 中创建一个名为 navigation 的服务并将我的导航放入一个新的配置键中:

On how to use navigation: I'd create a service in the servicemanager called navigation and put my navigation into a new config-key:

return array(
    'navigation' => array(
        // your navigation config goes here
    ),
    'servicemanager' => array(
        'factories' => array(
            'navigation' => function($sm) {
                $config = $sm->get('Configuration');
                $navigation = new \Zend\Navigation\Navigation($config->get('navigation'));
                return $navigation;
            }
        ),
    ),
    'view_helpers' => array(
        'factories' => array(
            'navigation' => function($sm) {
                return new \My\View\Helper\Navigation($sm->get('navigation'));
            }
        ),
    ),
);

(不确定导航类名称.还没有看过它们.)

(Not sure about the navigation class names. Haven't had a look on them.)

这做了三件事:

  • 提供一个名为 navigation 的服务,指向实际的实例如果 Zend 的导航
  • 提供您的视图助手并将其传递给导航实例的引用
  • 创建一个包含导航配置的新顶级键 navigation.其他模块可以在此处添加自定义导航,而无需更改设置中的任何内容.
  • Provide a service called navigation pointing to the actual instance if zend's navigation
  • Provide your view helper and hand it a reference to the instance of your navigation
  • Create a new top-level key navigation containing the navigation configuration. Other modules can add custom navigation here without changing anything in your setup.

例如在您的控制器中,您通过调用代码获取导航实例

For example in your controllers you code fetch the navigation instance by calling

$this->getServiceLocator()->get('navigation');

而在您的视图中,助手可以通过其构造函数访问导航:

while in your view helper has access to the navigation by it's constructor:

Navigation extends // ...
{
    public function __construct($navigation)
    {
        // do anything you want
    }
}

其他模块可以通过写入相同的键向您的导航添加条目:

Other modules can add entries to your navigation by writing into the same key:

return array(
    'navigation' => array(
        // further navigation entries
    ),
);

您将初始逻辑放在哪里(例如设置服务/视图助手)取决于您.我更喜欢为此编写一个自己的模块,可以用一行代码禁用它(导致导航不再存在).但是默认模块也可能是一个好地方.最终,您可以为导航和视图助手创建自己的工厂类,而不是将配置与您的代码混合在一起.

Where you put the initial logic (e.g. setting up the services/view helpers) is up to you. I prefer writing a own module for this which can be disabled with a single line of code (resulting in the navigation not being present anymore). But the default module is probably a good place as well. Ultimately you could create your own factory-classes for the navigation and view helper instead of mixing the configuration with your code.

免责声明:代码只是草稿 - 未经测试.

Disclaimer: Code's just a draft - not tested.

这篇关于Zend 框架 2:Zend_Navigation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆