I want to get service locator in my custom plugin. I create it through a factory in 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;
}
]
];
}
...and my plugin:
<?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;
}
}
Then I calling getAppInfo() in my controller:
$appPlugin = new AppPlugin();
$appPlugin->getAppInfo();
...and I get the error:
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
But if I'm passing service locator from my controller it works fine:
$appPlugin = new AppPlugin();
$appPlugin->setLocator($this->getServiceLocator());
$appPlugin->getAppInfo();
I'll be glad if someone will explain me what I'm doing wrong. Thanks.