# ZF3 Get viewhelper render in controller

**URL:** <https://discourse.laminas.dev/t/zf3-get-viewhelper-render-in-controller/1071>\
**Category:** Components & MVC\
**Created:** [May 28, 2019, 2:29pm UTC](https://discourse.laminas.dev/t/zf3-get-viewhelper-render-in-controller/1071 "2019-05-28T14:29:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alaksander\_A](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/alaksander_a/32/498_2.png) [@Alaksander\_A](https://discourse.laminas.dev/u/Alaksander_A)\
**Post date:** [May 28, 2019, 2:29pm UTC](https://discourse.laminas.dev/t/zf3-get-viewhelper-render-in-controller/1071/1 "2019-05-28T14:29:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tnrn9b](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tnrn9b/32/505_2.png) [@tnrn9b](https://discourse.laminas.dev/u/tnrn9b)\
**Post date:** [March 22, 2020, 1:33pm UTC](https://discourse.laminas.dev/t/zf3-get-viewhelper-render-in-controller/1071/2 "2020-03-22T13:33:21Z")

</div>

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 _Your_Controller 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
}

```

}

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 22, 2020, 3:28pm UTC](https://discourse.laminas.dev/t/zf3-get-viewhelper-render-in-controller/1071/3 "2020-03-22T15:28:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xerkus](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/xerkus/32/22_2.png) [@xerkus](https://discourse.laminas.dev/u/xerkus)\
**Post date:** [March 22, 2020, 3:36pm UTC](https://discourse.laminas.dev/t/zf3-get-viewhelper-render-in-controller/1071/4 "2020-03-22T15:36:52Z")

</div>

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](https://github.com/mezzio/mezzio-laminasviewrenderer/blob/master/src/LaminasViewRenderer.php)

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 22, 2020, 3:37pm UTC](https://discourse.laminas.dev/t/zf3-get-viewhelper-render-in-controller/1071/5 "2020-03-22T15:37:58Z")

</div>

> [@xerkus](#):
>
> This is almost an year old post, so answer is probably no longer needed.

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