从视图助手调用自定义插件

Is there any way to call custom plugin from view helper in ZF3?

As per ZF3, the Factory for the Helper is created by me.

In ZF2 this is how we call the Plugin.

$ecommercePlugin = $this->getServiceLocator()
->get('ControllerPluginManager')
->get('CustomPlugin');

As serviceLocator is removed from ZF3, how to call the plugin?

Edit module.config

'view_helpers' => array(
    'invokables' => array(
        'CustomPlugin' => \MyMethod\Controller\Plugin\CustomPlugin::class
    )
),

MymethodController\CustomPlugin

class CustomPlugin extends AbstractPlugin
{
     //My methods
}

I would recommend registering the CustomPlugin with the ViewHelperManager. You can find information about configuration in Zend's Manual. This is typically how your Module's module.config.php may look:

<?php
    return [
        'controller_plugins' => [
            'invokables' => [
                'CustomPlugin' => \MyMethod\Controller\Plugin\CustomPlugin::class
            ],
        ],
    ];

To register it for use as a view helper you would add:

<?php
    return [
        'controller_plugins' => [
            'invokables' => [
                'CustomPlugin' => \MyMethod\Controller\Plugin\CustomPlugin::class
            ],
        ],
        'view_helpers' => [
            'invokables' => [
                'CustomPlugin' => \MyMethod\Controller\Plugin\CustomPlugin::class 
            ]
        ],
    ];

This new config will allow you to call $this->CustomPlugin() from both controllers and views. Now, you may run into a situation in which you need to inject dependencies into your plugin/view helper. In this case you can simply create a factory class to handle finding and injecting the dependencies and change your config to:

<?php
    return [
        'controller_plugins' => [
            'factories' => [
                'CustomPlugin' => \MyMethod\Controller\Plugin\CustomPlugin::class
            ],
        ],
        'view_helpers' => [
            'factories' => [
                'CustomPlugin' => \MyMethod\Controller\Plugin\CustomPlugin::class 
            ]
        ],
    ];

Now when the plugin/view helper is called, the factory will run and assemble the desired class. Let me know if you have any questions. There is a lot of flexibility in the config and with specific requirements or an example of the CustomPlugin class and your Module.php/module.config.php config could provide a more accurate example of what you are looking for.