Ildar
Ildar

Reputation: 796

How to get view from view helper in Zend Framework 2?

I can do so:

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

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

Can I get view object in view helper without passing it as a parameter in view? Like this:

// 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);
}

Upvotes: 0

Views: 2224

Answers (1)

fisharebest
fisharebest

Reputation: 1350

use Zend\View\Helper\AbstractHelper;

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

Upvotes: 2

Related Questions