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
CustomPluginwith theViewHelperManager. You can find information about configuration in Zend's Manual. This is typically how your Module'smodule.config.phpmay look:To register it for use as a view helper you would add:
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: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
CustomPluginclass and yourModule.php/module.config.phpconfig could provide a more accurate example of what you are looking for.