douou8954 2015-05-25 09:02
浏览 26

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

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.

  • 写回答

1条回答 默认 最新

  • dongxie8906 2015-05-25 09:21
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示