Dependency injection zend 2:尝试在我的视图帮助程序中使用zend\Navigation
我正在尝试使用Zend\Navigation从视图帮助器中的模板创建菜单栏 我越来越近了,用我现在的代码编辑了这个帖子 以下是视图帮助器:Dependency injection zend 2:尝试在我的视图帮助程序中使用zend\Navigation,dependency-injection,zend-framework2,Dependency Injection,Zend Framework2,我正在尝试使用Zend\Navigation从视图帮助器中的模板创建菜单栏 我越来越近了,用我现在的代码编辑了这个帖子 以下是视图帮助器: <?php namespace Helpdesk\View\Helper; use Zend\View\Helper\AbstractHelper; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorInterfac
<?php
namespace Helpdesk\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Navbar extends AbstractHelper implements ServiceLocatorAwareInterface {
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
return $this;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
public function __invoke() {
$partial = array('helpdesk/helpdesk/subNavTest.phtml','default');
$navigation = $this->getServiceLocator()->get('navigation');
$navigation->menu()->setPartial($partial);
return $navigation->menu()->render();
}
}
但是,当我像这样在视图中显示它时,它只显示部分模板,而不显示module.config.php中的导航配置
如果我在视图中正确执行以下操作,它将与我设置的配置一起显示:
<?php $partial = array('helpdesk/helpdesk/subNavTest.phtml','default') ?>
<?php $this->navigation('navigation')->menu()->setPartial($partial) ?>
<?php echo $this->navigation('navigation')->menu()->render() ?>
为什么我的视图助手没有进入导航配置
如果我在视图中正确执行以下操作,它将与我设置的配置一起显示:
<?php $partial = array('helpdesk/helpdesk/subNavTest.phtml','default') ?>
<?php $this->navigation('navigation')->menu()->setPartial($partial) ?>
<?php echo $this->navigation('navigation')->menu()->render() ?>
是的,这是因为在您的视图(工作的代码)中,您告诉导航助手在这一行使用名为navigation
的菜单容器
<?php $this->navigation('navigation')->menu()->setPartial($partial) ?>
^^^^^^^^^^- This is the menu container
或者,让助手在其\u invoke
方法中接受一个参数,该参数可以传递给助手
public function __invoke($container) {
$partial = array('helpdesk/helpdesk/subNavTest.phtml','default');
$navigation = $this->getServiceLocator()->get('navigation');
// tell navigation which container to use
$navigation($container)->menu()->setPartial($partial);
return $navigation->menu()->render();
}
在你看来,这就是
<?php echo $this->navbar('navigation'); ?>
<?php echo $this->navbar('navigation'); ?>