you just need to make your listener implements the 'ServiceLocatorAwareInterface' adding this 'use' block at the top of your service class:
use Zend\ServiceManager\ServiceLocatorAwareInterface,
Zend\ServiceManager\ServiceLocatorInterface;
then add the following property and methods to your class
/**
* @var ServiceLocatorInterface
*/
protected $serviceManager;
/**
*
* @param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceManager = $serviceLocator;
}
/**
*
* @return \Zend\ServiceManager\ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceManager;
}
Then, create your listener service, it can be an Invokable or a Factory if you need other configuration.
'Path\To\MyListener' => new MyListener(),
Zend Framework will now inject the service locator for you.