0

我正在一个带有“普通”菜单和子菜单的网站上工作。

这是引导程序中适用于导航的方法:

引导程序.php

protected function _initNavigation()
{        
    $view = $this->getResource('view');

    $view->addHelperPath(APPLICATION_ROOT . '/library/Pcw/View/Helper/Navigation', 'Pcw_View_Helper_');          

    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');

    $navigation = new Zend_Navigation($config);

    $view->navigation($navigation);

    $menuPartial = array('menu.phtml', 'default');        
    $view->navigation()->menu()->setPartial($menuPartial);

    $subMenuPartial = array('submenu.phtml', 'default');        
    $view->navigation()->subMenu()->setPartial($subMenuPartial);
} 

library/Pcw/View/Helper/Navigation/SubMenu.php

class Pcw_View_Helper_Navigation_SubMenu extends Zend_View_Helper_Navigation_Menu
{
    public function subMenu(Zend_Navigation_Container $container = null)
    {
        return parent::menu($container);
    }
}

根据 Stackoverflow 上的文档和主题,这种结构应该可以工作,但是会出现以下错误:

Fatal error:  Uncaught exception 'Zend_Navigation_Exception' with message 'Bad method call: Unknown method Zend_Navigation::subMenu' in /home/prj/domains/prjon.nl/library/Zend/Navigation/Container.php:366
Stack trace:
#0 [internal function]: Zend_Navigation_Container->__call('subMenu', Array)
#1 [internal function]: Zend_Navigation->subMenu()
#2 /home/prj/domains/prjon.nl/library/Zend/View/Helper/Navigation/HelperAbstract.php(503): call_user_func_array(Array, Array)
#3 /home/prj/domains/prjon.nl/library/Zend/View/Helper/Navigation.php(132): Zend_View_Helper_Navigation_HelperAbstract->__call('subMenu', Array)
#4 /home/prj/domains/prjon.nl/Application/Bootstrap.php(410): Zend_View_Helper_Navigation->__call('subMenu', Array)
#5 /home/prj/domains/prjon.nl/Application/Bootstrap.php(410): Zend_View_Helper_Navigation->subMenu()
#6 /home/prj/domains/prjon.nl/library/Zend/Application/Bootstrap/BootstrapAbstract.php(669): Bootstrap->_initNavigation() in /home/prj/domains/prjon.nl/library/Zend/Navigation/Container.php on line 366

错误很明显:找不到和调用方法 subMenu,但我创建了负责处理子菜单的助手。

有谁知道我做错了什么?

提前致谢!

4

3 回答 3

1

这就是我这样做的方式,希望它有意义。我省略了 ACL 和 Auth 集成(这是一个很棒的功能)。

假设我想要一个带有“home”和“admin”的顶部菜单,以及“admin”部分的子菜单。

在我的引导程序中:

protected function _initNavigation()
{
    $container = new Zend_Navigation();

    // or in xml etc
    $pages = array(
                array('label' => 'Home', 'controller' => 'index', 'action' => 'index'),
                array('label' => "Admin", 'controller' => 'admin', 'id' => 'admin', 'pages' => array(
                        array('label' => "Add an account", 'controller' => 'admin', 'action' => 'createuser'),
                        array('label' => "Clear cache", 'controller' => 'admin', 'action' => 'clearcache')
                        )
                )
    );

    $container->addPages($pages);

    Zend_Registry::set('Zend_Navigation', $container);
    return $container;
}

然后在我的布局中显示主菜单:

$container = Zend_Registry::get('Zend_Navigation');
$this->navigation()->menu()->renderMenu($container,array('minDepth' => 0, 'maxDepth' => 0))

(检查文档以获取所有 renderMenu 选项,非常灵活。您可以使用 'maxDepth' => 1 轻松使用管理子菜单进行翻转)

然后在我的管理模板中,显示子菜单:

$container = Zend_Registry::get('Zend_Navigation');
$found = $container->findById('admin');
echo $this->navigation()->menu()->renderMenu($found,array('minDepth' => 0))

这样,视图渲染与引导程序分离,我不需要局部视图或自定义视图助手。

于 2012-05-31T09:20:11.077 回答
1

我只是使用不同的路径语法,它适用于我的视图助手,addHelper() 应该与导航助手一起使用。

$view->addHelperPath(APPLICATION_ROOT . '/library/Pcw/View/Helper/Navigation', 'Pcw_View_Helper_'); 

改成:

$view->addHelperPath('/../library/Pcw/View/Helper/Navigation', 'Pcw_View_Helper_');

对我有用,希望对你也有用。

于 2012-05-31T07:19:26.310 回答
0

我在插件文件中的代码(ROOT/Application/SubmenuPlugin.php):

$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');

$submenuPartial = array('submenus/'.$currentControllerName.'.phtml', 'default'); //$currentControllerName defined a couple of lines ago             

$view->addHelperPath(APPLICATION_ROOT . '/library/Pcw/View/Helper', 'Pcw_View_Helper_');
$view->navigation()->submenu()->setPartial($submenuPartial);

位置子菜单:ROOT/Application/views/scripts/submenus/

位置子菜单查看助手:ROOT/library/Pcw/View/helper/Submenu.php

这对你有帮助吗?

于 2012-10-24T10:20:15.673 回答