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.