【问题标题】:zend 2: Trying to use Zend\Navigation in my view helperzend 2:尝试在我的视图助手中使用 Zend\Navigation
【发布时间】:2014-03-22 05:48:24
【问题描述】:

我正在尝试使用 Zend\Navigation 从视图助手中的模板创建菜单栏。

我离得更近了,并用我现在拥有的代码编辑了这个线程。

这里是视图助手:

<?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 中这样配置导航:

'view_helpers' => array(
    'invokables' => array(
        'navbar' => 'Helpdesk\View\Helper\Navbar',
    ),
),

'navigation' => array(
    'default' => array(
        array(
            'label' => 'One',
            'route' => 'link',
        ),
        array(
            'label' => 'Two',
            'route' => 'link',
        ),
        array(
            'label' => 'Three',
            'route' => 'link',
        ), ...

但是当我像 &lt;?php echo $this-&gt;navbar(); ?&gt; 这样在我的视图中显示它时,它只显示部分模板没有来自 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() ?>

为什么我的视图助手没有拉入导航配置?

【问题讨论】:

    标签: dependency-injection zend-framework2


    【解决方案1】:

    如果我在我的视图中正确执行以下操作,它会在我设置的配置中正常显示:

    是的,那是因为在您看来(有效的代码),您告诉导航助手在这一行使用名为 navigation 的菜单容器...

    <?php $this->navigation('navigation')->menu()->setPartial($partial) ?>
                             ^^^^^^^^^^- This is the menu container
    

    在您的 navbar 助手中,您没有指定菜单容器。如果您当时还没有使用导航助手,它没有菜单,并创建一个空菜单。

    你有两个选择,要么在调用你的助手之前告诉导航助手使用哪个容器

    // set the menu 
    <$php $this->navigation('navigation'); ?>
    // render helper
    <?php echo $this->navbar(); ?>
    

    或者,让您的助手在其__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'); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      相关资源
      最近更新 更多