我有导航菜单,它根据控制器的某些动作显示给用户。这是我正在做的事情。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 & Close</label>
<uri>#</uri>
</publish-unpublish-item>
<delete-item>
<label>Save & New</label>
<uri>#</uri>
</delete-item>
</item-new>
</configdata>
每个导航菜单的父元素表示例如
`<item-index>` represents item{controller}index{action}
`<item-new>` represents item{controller}new{action}
//and so on
这里是动作助手在上述navigation.xml文件的命名约定。 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));
将代码放在'_initView()'中会很有趣。我的猜测是你不会在你的引导方法中返回$ view'。 – Liyali 2012-04-22 11:18:50
很高兴看到你回来:),问题是我的应用程序由后端和一个前端以及一个Bootstrap文件组成。所以我想通过不添加任何代码来保持清洁。在bootstrap中,我试图保留只会被共享的资源。通过后端和前端。 – 2012-04-22 11:30:26
'_action-navigation.pthml'是否在你的模块之间共享,还是只在'admin /'模块中部分使用? – Liyali 2012-04-22 14:24:27