如何扩展 Zend 导航菜单视图助手?
我需要更改 Zend_View_Helper_Navigation_Menu
的输出。我找到了需要修改的两个函数,并且知道如何进行所需的更改。我不知道的是如何使导航对象使用我的视图助手而不是 Zend 的视图助手。
代表我的类扩展的代码片段:
// file /library/My/View/Helper/Navigation/Menu.php
class My_View_Helper_Navigation_Menu extends Zend_View_Helper_Navigation_Menu
{
protected function _renderDeepestMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth)
{
// modified code here
}
protected function _renderMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth,
$onlyActive)
{
// modified code here
}
}
编辑以澄清
我想更改
元素的类并删除 EOL
和缩进。菜单视图脚本没有选项可以做到这一点,这就是为什么我必须扩展它。在我的 Bootstrap 中初始化导航对象:
$navTable = new Default_Model_Site_DbTable_Navigation();
$view = $this->getResource('view');
$view->navigation(new Zend_Navigation($navTable->getNavigation()));
在布局中渲染菜单:
echo $this->navigation()->menu();
解决方案
我通过按如下方式重命名内容来使其工作,但我不清楚为什么不能重载/覆盖 _Menu
类和 menu()
函数。
- 将类名称更改为
My_View_Helper_Navigation_MyMenu
- 将
myMenu
函数添加到类中 (returnparent::menu($container);
) - 调用
echo $this->navigation()->myMenu();
在布局
类线框中:
// file /library/My/View/Helper/Navigation/MyMenu.php
class My_View_Helper_Navigation_MyMenu extends Zend_View_Helper_Navigation_Menu
{
public function myMenu(Zend_Navigation_Container $container = null)
{
return parent::menu($container);
}
protected function _renderDeepestMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth)
{
// modified code here
}
protected function _renderMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth,
$onlyActive)
{
// modified code here
}
}
I need to change the output of Zend_View_Helper_Navigation_Menu
. I've found the two functions that I'll need to modify, and I know how to make the changes I need. What I don't know is how to make the Navigation object use my view helper instead of the Zend one.
Code snippet representing my class extension:
// file /library/My/View/Helper/Navigation/Menu.php
class My_View_Helper_Navigation_Menu extends Zend_View_Helper_Navigation_Menu
{
protected function _renderDeepestMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth)
{
// modified code here
}
protected function _renderMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth,
$onlyActive)
{
// modified code here
}
}
Edits to Clarify
I want to change the class of the <li>
elements and remove the EOL
and indentation. There are no options to do that with the menu view script, which is why I'll have to extend it.
Initializing the navigation object in my Bootstrap:
$navTable = new Default_Model_Site_DbTable_Navigation();
$view = $this->getResource('view');
$view->navigation(new Zend_Navigation($navTable->getNavigation()));
Rendering the menu in my layout:
echo $this->navigation()->menu();
Solution
I got it working by renaming things as follows, but I am not clear on why I can't overload/overwrite the _Menu
class and menu()
function.
- Change the class name to
My_View_Helper_Navigation_MyMenu
- Add the
myMenu
function to the the class (return parent::menu($container);
) - Call
echo $this->navigation()->myMenu();
in the layout
Class wireframe:
// file /library/My/View/Helper/Navigation/MyMenu.php
class My_View_Helper_Navigation_MyMenu extends Zend_View_Helper_Navigation_Menu
{
public function myMenu(Zend_Navigation_Container $container = null)
{
return parent::menu($container);
}
protected function _renderDeepestMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth)
{
// modified code here
}
protected function _renderMenu(Zend_Navigation_Container $container,
$ulClass,
$indent,
$minDepth,
$maxDepth,
$onlyActive)
{
// modified code here
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自: 让 Zend_Navigation 菜单与 jQuery 的 Fisheye 一起使用
编辑
抱歉,我没有看到您的解决方案,这正是我发布的内容。
但为什么这不是菜单类的真正扩展呢?
From: Getting Zend_Navigation menu to work with jQuery's Fisheye
EDIT
Sorry I've not seen your solution, it is exactly what I've posted.
But why this is not a trully extension of menu class?
对于任何可能需要答案的人,我找到了更好的方法,并且可能是预期的方法。
您唯一需要做的就是创建您自己的自定义视图助手,扩展“Zend_View_Helper_Navigation_HelperAbstract”并将导航视图助手的默认代理设置为您自己的。
例如
完成此操作后
(我正在更改菜单控制器操作中的默认代理,因为它已添加到操作堆栈中)
,就可以在视图中使用它
注意:您仍然需要重命名视图助手类,但这就是视图助手在 Zend 中的工作方式(名称不应该冲突)。
For anyone who might need an answer I found a better way and probably the intended way.
The only thing you have to do is to create your own custom view helper that extends 'Zend_View_Helper_Navigation_HelperAbstract' and set default proxy for navigation view helper to your own.
E.g.
and
(I am changing default proxy in menu controller action, as it is added to action stack)
Having done that, it will be possible to use this in the view
Note: You still have to rename the view helper class, but that's how view helpers work in Zend (Names are not supposed to collide).
你编辑你的帖子了吗?现在看来我的回答与你的问题完全无关?
如果你说出你需要改变什么,那就更容易了。目前你的问题有点令人困惑。
我假设您想要在已经创建导航之后编辑视图。如果您能够在创建之前完成它,那么它会更容易。下面的这一点有点令人困惑,因为您通常会事先更改选项。
请原谅缩进,几乎很难将其对齐
请注意,我在上面使用了 $YOUR_NAVIGATION_OBJECT。仅当您在页面上使用多个导航时才使用它。否则,您只需使用 Render() 而不是 RenderMenu()。
did you edit your post? It seems like my answer is totally irrelevant to your question now?
If you say what you need to change it will be easier. At the moment your question is a little confusing.
I've assumed you want to edit the view AFTER you have already created your navigation. If you are able to do it before you create it, then its even easier. This bit below is a little confusing because you would normally change the options before hand.
excuse the indentation, it was really hard to get it lined nearly
Notice that I used $YOUR_NAVIGATION_OBJECT above. You only use that if you use more than one Navigation on your page. Otherwise, you jus use Render() instead of RenderMenu().