Zend_Navigation:如何将活动状态应用于控制器的所有操作?
Let's say, that we have:
$pages = array(
array(
'controller' => 'controller1',
'label' => 'Label1',
),
array (
'controller' => 'controller2',
'label' => 'Label2'
),
);
$container = new Zend_Navigation($pages);
When user clicks Label1, controller1/index action is rendered and Label1 becomes active state - everything is ok. On this page I have many links, such as: controller1/action1, controller1/action2, etc When one of these links is clicked, Label1 looses active state.
I understand, that I can add all sub-pages into Zend_Navigation, but there are plenty of these pages and I never need it anywhere for navigation, so, I'd prefer to have something like:
public function init()
{
$this->view->navigation()-> ... get item by label ... -> setActive();
}
inside controller1. Is it possible?
假设我们有: p>
$ pages = 数组(
数组(
'控制器'=>'controller1',
'标签'=>'Label1',
),
数组(
'controller'=>'controller2',
'label'=>'Label2'
),
);
$ container = new Zend_Navigation($ pages);
code> pre>
当用户 单击Label1,呈现controller1 / index动作,Label1变为活动状态 - 一切正常。
在此页面上我有很多链接,例如:controller1 / action1,controller1 / action2等
当点击其中一个链接时,Label1 失去活跃状态。 p>
据我所知,我可以将所有子页面添加到Zend_Navigation中,但是这些页面很多,我从来不需要任何导航,所以,我会 喜欢这样的东西: p>
public function init()
{
$ this-> view-> navigation() - > ...按标签获取项目... - > control1内的setActive();
}
code> pre>
。 有可能吗? p>
div>
Your init method is very close!
$page = $this->view->navigation()->findOneByLabel('Your Label'); /* @var $page Zend_Navigation_Page */
if ( $page ) {
$page->setActive();
}
I think this is exactly what he is looking for: Simply paste this into your menu.phtml or any .phtml where you print your menu:
// apply active state to all actions of controller
$controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
$page = $this->navigation()->findOneByController($controller); /* @var $page Zend_Navigation_Page */
if ( $page ) {
$page->setActive();
}
echo $this->navigation()->menu();
Of course you need to init first a navigation structure with Zend_Navigation_Page_Mvc pages. Somehow this does not work for me with url pages...