Zend Framework中基于行动的导航

时间:2012-04-22 09:53:15

标签: zend-framework


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

//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));

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

$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);