Call Plugin in other Plugin ZF2

2019-07-22 13:42发布

in Zend Framework 2 as I can recall a plugin module A plug-in module B.

If you recall the plugin from the controller, it works everywhere, but I need to call a plugin in another plugin.

How can I do?

3条回答
走好不送
2楼-- · 2019-07-22 13:58

Try loading the plugin from the Controller Plugin Manager.

PluginB

$pluginA = $this->serviceLocator->get('ControllerPluginManager')->get('pluginA');
// Invoke plugin as normal
$pluginA(params);
查看更多
欢心
3楼-- · 2019-07-22 14:05

You basically have to inject PluginA into PluginB. I.e:

$pluginA = new PluginA();
$pluginB = new PluginB($pluginA);
echo $pluginB("Hello World");

class PluginB {
    protected $pluginA;
    public function __construct(PluginA $pluginA) {
        $this->pluginA = $pluginA;
    }

    public function __invoke($arg) {
        $step1 = $this->doSomething($arg);
        return $this->pluginA->doSomeOtherPluginAThing($step1);
    } 
}

Ultimately your Solution would look a little different and you'd do the injection via ServiceManager Factories

查看更多
beautiful°
4楼-- · 2019-07-22 14:12

You can access controller from inside your plugin:

$this->getController()->anotherPlugin();
查看更多
登录 后发表回答