我想在我的自定义插件中获取服务定位器。我通过Module.php中的工厂创建它:
public function getControllerPluginConfig() {
return [
'factories' => [
'Application\Plugin\AppPlugin' => function($serviceManager) {
$serviceLocator = $serviceManager->getServiceLocator();
$appPlugin = new \Application\Plugin\AppPlugin();
$appPlugin->setLocator($serviceLocator);
return $appPlugin;
}
]
];
}
...和我的插件:
<?php
namespace Application\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
class AppPlugin extends AbstractPlugin {
protected $locator;
public function getAppInfo() {
$config = $this->locator->get('Config');
}
/**
* Set Service Locator
* @param \Zend\ServiceManager\ServiceManager $locator
*/
public function setLocator($locator) {
$this->locator = $locator;
}
}
然后我在我的控制器中调用getAppInfo():
$appPlugin = new AppPlugin();
$appPlugin->getAppInfo();
......我收到错误:
Fatal error: Call to a member function get() on a non-object in /vagrant/app/module/Application/src/Application/Plugin/AppPlugin.php on line 13
Call Stack
# Time Memory Function Location
1 0.0027 232104 {main}( ) ../index.php:0
2 0.6238 3166904 Zend\Mvc\Application->run( ) ../index.php:21
3 1.4410 5659112 Zend\EventManager\EventManager->trigger( ) ../Application.php:314
4 1.4410 5659112 Zend\EventManager\EventManager->triggerListeners( ) ../EventManager.php:205
5 1.4411 5660872 call_user_func ( ) ../EventManager.php:444
6 1.4411 5661440 Zend\Mvc\DispatchListener->onDispatch( ) ../EventManager.php:444
7 1.4505 5704600 Zend\Mvc\Controller\AbstractController->dispatch( ) ../DispatchListener.php:93
8 1.4505 5705080 Zend\EventManager\EventManager->trigger( ) ../AbstractController.php:118
9 1.4505 5705080 Zend\EventManager\EventManager->triggerListeners( ) ../EventManager.php:205
10 1.4507 5716656 call_user_func ( ) ../EventManager.php:444
11 1.4507 5716784 Zend\Mvc\Controller\AbstractActionController->onDispatch( ) ../EventManager.php:444
12 1.4508 5717504 Application\Controller\IndexController->indexAction( ) ../AbstractActionController.php:82
13 1.4641 5727768 Application\Plugin\AppPlugin->getAppInfo( ) ../IndexController.php:21
但是,如果我从我的控制器传递服务定位器,它可以正常工作:
$appPlugin = new AppPlugin();
$appPlugin->setLocator($this->getServiceLocator());
$appPlugin->getAppInfo();
如果有人能解释我做错了什么,我会很高兴的。感谢。
答案 0 :(得分:2)
如果可能的话,您不应该尝试访问除工厂之外的任何类中的ServiceLocator。这样做的主要原因是如果将ServiceLocator注入到您的类中,您现在不知道该类的依赖项是什么,因为它现在可能包含任何内容。
关于依赖注入,您有两个基本选择:构造函数或setter注入。根据经验,总是更喜欢构造函数注入。 Setter注入只应用于可选的依赖项,并且它还会使您的代码更加模糊,因为该类现在是可变的。如果你使用纯粹的构造函数注入,你的依赖项是不可变的,你总是可以确定它们会在那里。
除了使用闭包之外,通常更好的方法是使用具体的工厂类,因为闭包不能被操作代码缓存,如果它包含闭包,你的配置数组也不能被缓存。
有关如何设置类以实现FactoryInterface
的详细说明,请参阅https://stackoverflow.com/a/18866169/1312094
话虽如此,为了讨论的目的,让我们坚持你的封闭工厂的例子。而不是注入ServiceManager,我们注入了配置数组(这仍然有点太依赖于上帝;更好地注入你需要的配置的特定键,或者更好的是,实例化的类)。
public function getControllerPluginConfig() {
return [
'factories' => [
\Application\Plugin\AppPlugin::class => function($serviceManager) {
$serviceLocator = $serviceManager->getServiceLocator();
$config = $serviceLocator->get('Config');
$appPlugin = new \Application\Plugin\AppPlugin($config);
return $appPlugin;
}
]
];
}
这里插件被修改为在构造函数中获取配置。我假设这个插件代码只是一个概念证明,并且除了返回配置之外,你实际上会对插件做一些有用的事情,但希望这确实显示了注入插件依赖项的模式。
<?php
namespace Application\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
class AppPlugin extends AbstractPlugin {
protected $config;
public function __construct($config){
$this->config = $config;
}
public function getAppInfo() {
return $this->config;
}
}
另外,一件小事,但假设PHP 5.5+,请考虑使用\Application\Plugin\AppPlugin::class
,就像我在上面的示例中所做的那样,而不是使用配置键的字符串文字。
答案 1 :(得分:1)
我是通过ServiceLocatorAwareInterface创建它并使其成为可调用的:
namespace Application\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class AppPlugin extends AbstractPlugin implements ServiceLocatorAwareInterface {
/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator;
/**
* Get system information
* @return type
*/
public function getAppInfo() {
$config = $this->getServiceLocator()->get('Config');
// to do something...
}
/**
* Retrieve service manager instance
*
* @return ServiceLocator
*/
public function getServiceLocator() {
return $this->serviceLocator->getServiceLocator();
}
/**
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
}
然后在module.appplication.config中:
'controller_plugins' => [
'invokables' => [
'AppPlugin' => \Application\Controller\Plugin\AppPlugin::class
]
]
...现在我可以在控制器中使用我的插件:
$app = $this->AppPlugin()->getAppInfo();
答案 2 :(得分:0)
您无法直接在控制器中实例化该类,您需要使用密钥“Application \ Plugin \ AppPlugin”将其从控制器插件管理器中取出
$pluginManager = $this->getPluginManager();
$appPlugin = $pluginManager->get('Application\Plugin\AppPlugin');
$appPlugin->getAppInfo();
或者您可以将方法更改为:
public function getAppInfo()
{
$serviceManager = $this->controller->getServiceLocator();
$config = $serviceManager->get('Config');
}