Zend Framework 中基于动作的导航 [英] Action based navigation in Zend Framework
问题描述
我有导航菜单,根据控制器的某些操作向用户显示.这就是我正在做的事情.
//在我想要动作导航菜单"的每个控制器动作中公共函数 indexAction(){$this->_helper->navigation()->renderActionNavigation();}公共函数 newAction(){$this->_helper->navigation()->renderActionNavigation();}
相应地显示导航菜单.这是我的 navigation.xml 文件.
<uri>#</uri></publish-unpublish-item><删除项目><标签>保存&新建</标签><uri>#</uri></delete-item></item-new></configdata>
例如上面navigation.xml文件中每个导航菜单的父元素代表一个命名约定
`` 代表 item{controller}index{action}`<item-new>` 代表 item{controller}new{action}//等等
这里是动作助手.我正在使用的 Navigation.php
class Zend_Controller_Action_Helper_Navigation 扩展 Zend_Controller_Action_Helper_Abstract{私人 $_view = null;公共函数直接(){$this->_view = Zend_Layout::getMvcInstance()->getView();$this->_view->placeholder('action-navigation');返回 $this;}公共函数 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
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;?>
<?php $this->placeholder('action-navigation')->captureEnd();?>
我的目录结构如下
/应用程序/布局管理文件默认.phtml/模块/行政/控制器/帮手/导航.php索引控制器.php/视图/帮手/脚本/部分_action-navigation.pthml侧边栏.phtml/指数/物品
我遇到的奇怪行为是.在我的 Bootstrap.php 文件中有一个空的 _initView() 方法.如果此方法存在,我的应用程序可以正常工作.注意这个方法是空的.但是当我删除它时,它给了我以下错误.
应用程序错误异常信息:消息:在路径 (./views/scripts/) 中找不到脚本partials/_action-navigation.phtml"
我无法通过 Zend Framework 理解这种行为.动作导航代码如何与 Bootstrap 中的 _initView 方法相关?发生了什么以及对此有什么解决方法或任何改进我的代码的建议?
更新:问题出在这行代码
$this->_view->partial('partials/_action-navigation.phtml', array('container' => $container));
您忘记添加 admin
模块的名称作为第二个参数:
$this->_view->partial('partials/_action-navigation.phtml', 'admin', array('container' => $container));
i have navigation menu which is displayed to users based on certain action of the controller. here is what i am doing.
//in each controller-action where i want "action navigation menu"
public function indexAction()
{
$this->_helper->navigation()->renderActionNavigation();
}
public function newAction()
{
$this->_helper->navigation()->renderActionNavigation();
}
the navigation menu is displayed accordingly. here is my navigation.xml file.
<?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>
the parent element of each navigation menu represents a naming convention in the above navigation.xml file for example
`<item-index>` represents item{controller}index{action}
`<item-new>` represents item{controller}new{action}
//and so on
here is the action helper. Navigation.php i am using
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));
}
}
and finally _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(); ?>
My directory structure is as follows
/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
the weird behavior i am experiencing is. in my Bootstrap.php file there is an empty _initView() method. my application works properly if this method exist. note that this method is empty. but when i remove it it gives me following error.
Application error
Exception information:
Message: script 'partials/_action-navigation.phtml' not found in path (./views/scripts/)
i am not able to understand this behavior by Zend Framework. how is action-navigation code related to _initView method in Bootstrap? what is happening and any fix for this or any suggestion for improvement of my code?
Update: The problem lies with this line of code
$this->_view->partial('partials/_action-navigation.phtml', array('container' => $container));
You forgot to add the name of the admin
module as second argument:
$this->_view->partial('partials/_action-navigation.phtml', 'admin', array('container' => $container));
这篇关于Zend Framework 中基于动作的导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!