简体   繁体   中英

ZF2 view plugin manager not merging config

On my module.config.php I've got something like:

 return [
       'view_helpers' => [
           'invokables' => [
               'mycustomviewhelper' => 'Namespace\View\Helper\MyCustomViewHelper',
           ],
       ],
   ];

I have also got a utility class that will handle the responsibility of rendering a helper. Something like Zend\\Paginator.

Zend\\Paginator has a __toString() method that proxies to render() call, which instantiates View\\Renderer\\PhpRenderer() and then calls $view->paginationControl($this) .

I am trying to replicate the similar functionality in my utility class, which has similar strategy to what Zend\\Paginator already does, the only thing being different is my view helper is a custom one. Hence, my code looks like:

$view->MyCustomViewHelper($this);

This does not work, because the PhpRenderer ignores the config defined manually and does the following in getHelperPluginManager:

$this->setHelperPluginManager(new HelperPluginManager());

I've tried invoking the helpers already defined in ViewHelperManager and this works well.

I did try merging in the config beforehand and then setting the PhpRenderer in the view but then this caused other problems, such as my partials were not found etc.

Now my question is why does ZF not consider any custom registered views when trying to render it in isolation. Is there any other way to do this?

Thank you.

Right, after a bit of a debugging, and playing with the configs, I was able to make this work. Still not sure if this is the best way to do this, but looks like currently there's no other way to make it work.

I created a factory for the utility class, instantiated the PhpRenderer, and then merged in my config with the ViewPluginManager manually. My factory now looks like:

public function createService(ServiceLocatorInterface $serviceLocatorInterface) 
{
    $dataTable = new DataTable;

    $phpRenderer = new PhpRenderer();
    $config = new \Zend\ServiceManager\Config([
        'invokables' => [
            'datatablerenderer' => 'DataTable\View\Helper\DataTableRenderer'
        ],
    ]);

    $phpRenderer->setHelperPluginManager(new HelperPluginManager($config));
    $dataTable->setView($phpRenderer);
    return $dataTable;
}

However will have to refactor this so that the config comes from the view_helpers config key and is not hardcoded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM