如何从Zend Framework 2中的视图助手获取视图?

时间:2013-03-20 11:08:07

标签: php zend-framework2

我可以这样做:

// View
<div>
<?php echo $this->myHelper($this); ?>
</div>

// View helper
public function __invoke($view)
{
   ...
   return $view->partial('page_nav', $someData);
}

我可以在视图助手中获取视图对象而不将其作为视图中的参数传递吗?像这样:

// View
<div>
<?php echo $this->myHelper(); ?>
</div>

// View Helper factory
public function createService(ServiceLocatorInterface $pluginManager)
{        
    ...     
    $view = foo(); // maybe here I can get view?
    return new myHelper($view);
}

// View helper
public function __invoke()
{
   ...
   $view = $this->view;
   return $view->partial('page_nav', $someData);
}

1 个答案:

答案 0 :(得分:2)

use Zend\View\Helper\AbstractHelper;

class MyHelper extends AbstractHelper {
    public function __invoke() {
        $view = $this->getView();
        // Do something with $view
    }
}