ZF3 Get viewhelper render in controller

How I can to get viewhelper (or other way) part of layout (.phtml) in other place — Service, Controller, render with params and put rendered layout in variable?

1 Like

Odd option: inject the plugin as a dependency into your class factory

class ClassFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/**@var HelperPluginManager $helperPluginManager */
$pluginManager = $container->get(‘ViewHelperManager’);
$plugin = $pluginManager->get(‘plugin’);
return new Class($plugin);
}

Alternatively:

class YourController extends AbstractActionController
{

public function indexAction()
{
         $navigation = $this->getPluginManager()->get('navigation');
         $html = $navigation('your-nav')->menu('your-nav')
                            ->setMinDepth(0)->setPartial('menu.phtml')->renderPartial();
               return new ViewModel();
}

public function getPluginManager()
{
    return parent::getPluginManager(); // TODO: Change the autogenerated stub
}

}

If you can describe your goal that you are trying to solve, then we can help you with a concrete solution. At the moment nobody can say if the access to a view helper is needed and the right way.

This is almost an year old post, so answer is probably no longer needed.

I think approach that would work the best is to create service that can then be injected where some templates need to be rendered.
For the service implementation inspirations, I believe, LaminasViewRenderer used in Mezzio is a good starting point: https://github.com/mezzio/mezzio-laminasviewrenderer/blob/master/src/LaminasViewRenderer.php

Right, the original post is old! I missed that.