1


我有导航菜单,根据控制器的某些操作向用户显示。这就是我正在做的事情。

//in each controller-action where i want "action navigation menu"
public function indexAction()
{
    $this->_helper->navigation()->renderActionNavigation();
}
public function newAction()
{
    $this->_helper->navigation()->renderActionNavigation();
}

相应地显示导航菜单。这是我的navigation.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
    <item-index>
        <new-item>
            <label>New Item</label>
            <route>admin-item-new</route>
        </new-item>
        <delete-item>
            <label>Delete Item</label>
            <uri>#</uri>
        </delete-item>
    </item-index>
    <item-new>
        <publish-unpublish-item>
            <label>Save &amp; Close</label>
            <uri>#</uri>
        </publish-unpublish-item>
        <delete-item>
            <label>Save &amp; New</label>
            <uri>#</uri>
        </delete-item>
    </item-new>
</configdata>

每个导航菜单的父元素代表上述navigation.xml文件中的命名约定,例如

`<item-index>` represents item{controller}index{action}
`<item-new>` represents item{controller}new{action}
//and so on

这是动作助手。我正在使用的 Navigation.php

class Zend_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
    private $_view = null;

    public function direct()
    {
        $this->_view = Zend_Layout::getMvcInstance()->getView();
        $this->_view->placeholder('action-navigation');
        return $this;
    }

    public function renderActionNavigation()
    {   
        $config = new Zend_Config_Xml(
            APPLICATION_PATH.'/configs/navigation.xml', strtolower(
                $this->getRequest()->getControllerName().'-'.
                $this->getRequest()->getActionName()
            )
        );
        $container = new Zend_Navigation($config);
        $this->_view->partial('partials/_action-navigation.phtml', array('container' => $container));
    }
}

最后_action-navigation.phtml

<?php $this->placeholder('action-navigation')->captureStart(); ?>
<div class="statsRow">
    <div class="wrapper" >
        <?php foreach($this->container as $page): ?>
            <a href="<?php echo $page->getHref(); ?>" class="<?php echo $page->class; ?>" id="<?php echo $page->id; ?>"></a>
        <?php endforeach; ?>
     </div>
</div>
<?php $this->placeholder('action-navigation')->captureEnd(); ?>

我的目录结构如下

/application
  /layouts
    admin.phtml
    default.phtml    
  /modules
    /admin
      /controllers
        /helpers
          /Navigation.php
        IndexController.php
      /views
        /helpers
        /scripts
          /partials
            _action-navigation.pthml
            sidebar.phtml
          /index
          /item

我正在经历的奇怪行为是。在我的 Bootstrap.php 文件中有一个空的 _initView() 方法。如果此方法存在,我的应用程序将正常工作。请注意,此方法为空。但是当我删除它时,它会给我以下错误。

Application error

Exception information:

Message: script 'partials/_action-navigation.phtml' not found in path (./views/scripts/)

我无法理解 Zend Framework 的这种行为。与 Bootstrap 中的 _initView 方法相关的操作导航代码如何?发生了什么,对此有何修复或对改进我的代码有任何建议?

更新: 问题出在这行代码上

$this->_view->partial('partials/_action-navigation.phtml', array('container' => $container));
4

2 回答 2

1

您忘记添加admin模块的名称作为第二个参数:

$this->_view->partial('partials/_action-navigation.phtml', 'admin', array('container' => $container));
于 2012-04-23T06:15:47.530 回答
0

我建议你使用这种干净和轻量级的方式,消除你对通知实际模块的依赖,并在没有 captureStart 和 End 的情况下保持你的视图脚本干净(它使用 ob_start ...当你执行 captureStart-end 时)

$navigation = $this->view->navigation()->menu()->renderPartial($container, 'partials/_action-navigation.phtml');
// U can use placeholders `append` and `prepend` methods too, if u need more control in your placeholder content
$this->view->placeholder('action-navigation')->set($navigation);
于 2012-05-17T06:54:58.290 回答