ZF2从Controller插件工厂获取ServiceManager

Chr*_*man 1 php zend-framework2

我正在尝试使用工厂创建一个新的Controller插件来注入依赖项.

public function createService(ServiceLocatorInterface $serviceLocator) {

    $services = $serviceLocator->getServiceLocator();
    /** @var \Zend\Mvc\Controller\PluginManager */

    $plugin = new MyPlugin();

    if ($services->has('my_service')) {
        $plugin->setService($services->get('my_service'));
    }

    return $plugin;
}
Run Code Online (Sandbox Code Playgroud)

问题是$服务找不到'my_service'

我在服务管理器中添加了正确的配置

'services' => array(
    'invokables' => array(
        'my_service' => 'Application\Service\MyService'
    )
),
'controller_plugins' => array(
    'factories' => array(
        'my_plugin' => 'Application\Controller\Plugin\Factory\MyPlugin'
    )
)
Run Code Online (Sandbox Code Playgroud)

我的想法是PluginManager中的一个错误,它没有正确地注入服务管理器.

Cri*_*isp 5

我在服务管理器中添加了正确的配置

服务管理器配置的关键是service_manager,不是services为什么找不到你的invokable,更改密钥......

'service_manager' => array(
    'invokables' => array(
        'my_service' => 'Application\Service\MyService'
    )
),
// ...
Run Code Online (Sandbox Code Playgroud)