0

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
}
halfpastfour.am
  • 5,764
  • 3
  • 44
  • 61
Keyur
  • 1,113
  • 1
  • 23
  • 42

1 Answers1

0

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.

Curtis Kelsey
  • 716
  • 8
  • 32
  • I have configured plugin as your suggestion. But didn't worked for me. Error >Call to Undefined Method ::CustomPlugin() – Keyur Mar 28 '17 at 13:09
  • Can you post your plugin class and config? – Curtis Kelsey Mar 28 '17 at 13:27
  • Is there a constructor in the plugin with any dependencies being injected? Or, are there any being loaded with an initializer that you know of? – Curtis Kelsey Mar 28 '17 at 13:39
  • No sir. There is not any dependencies. – Keyur Mar 28 '17 at 13:44
  • I would double-check the namespacing of your class. I noticed your update has the namespace as `MymethodController\CustomPlugin` but the module config has `\MyMethod\Controller\Plugin\CustomPlugin::class` – Curtis Kelsey Mar 28 '17 at 13:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139257/discussion-between-curtis-kelsey-and-keyur-k). – Curtis Kelsey Mar 28 '17 at 13:47