我可以这样做:
// 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);
}
答案 0 :(得分:2)
use Zend\View\Helper\AbstractHelper;
class MyHelper extends AbstractHelper {
public function __invoke() {
$view = $this->getView();
// Do something with $view
}
}