简体   繁体   English

ZF2视图插件管理器未合并配置

[英]ZF2 view plugin manager not merging config

On my module.config.php I've got something like: 在我的module.config.php上,我有类似以下内容:

 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之类的东西。

Zend\\Paginator has a __toString() method that proxies to render() call, which instantiates View\\Renderer\\PhpRenderer() and then calls $view->paginationControl($this) . Zend \\ Paginator有一个__toString()方法,可以代理render()调用,该方法实例化View\\Renderer\\PhpRenderer() ,然后调用$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. 我正在尝试在我的实用程序类中复制类似的功能,该类具有与Zend \\ Paginator已经执行的策略类似的策略,唯一不同的是我的视图助手是自定义的。 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: 这是行不通的,因为PhpRenderer会忽略手动定义的配置,并在getHelperPluginManager中执行以下操作:

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

I've tried invoking the helpers already defined in ViewHelperManager and this works well. 我尝试调用已经在ViewHelperManager中定义的助手,并且效果很好。

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. 我确实尝试过事先在配置中合并,然后在视图中设置PhpRenderer,但是这引起了其他问题,例如找不到我的部件等。

Now my question is why does ZF not consider any custom registered views when trying to render it in isolation. 现在,我的问题是,为什么ZF在尝试单独呈现时不考虑任何自定义注册视图。 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. 我为实用程序类创建了一个工厂,实例化了PhpRenderer,然后在配置中手动将其与ViewPluginManager合并。 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. 但是,必须重构此配置,以便配置来自view_helpers配置键,并且不会进行硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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