ZF2 ServiceManager

I am trying to use the ServiceManager from zf2 without the MVC module. I have two files: classServiceManager.php and sm.php.

1) classServiceManager.php:

namespace ZF2;

use Zend\ServiceManager\ServiceManager; 
use Zend\ServiceManager\ServiceLocatorAwareInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 
use \InvalidArgumentException;  

class classServiceManager implements ServiceLocatorAwareInterface{
    protected $serviceLocator;

    function __construct(){
        //echo "SM: ". $this->getServiceLocator()->get('sm');
        echo "SM: ".$this->serviceLocator->get('sm');

    }

    function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
      }

    function getServiceLocator() {
        return $this->serviceLocator;
    }       

}

2) sm.php

$config = array(...);

require_once 'Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory($config); 
......  
use ZF2\classServiceManager;

$serviceManager = new ServiceManager();
$serviceManager->setService('sm', 'aaa');   

$a = new classServiceManager();

But when I start sm.php, I get an error message:

Fatal error: Call to a member function get() on a non-object....
+4
source share
2 answers

By default, the Zend Framework MVC registers an initializer that will deploy an instance of ServiceManager, which is an implementation of Zend \ ServiceManager \ ServiceLocatorInterface, to any class that implements Zend \ ServiceManager \ ServiceLocatorAwareInterface.

therefore, ServiceLocatorAwareInterface is used only in the MVC application.

ServiceManager zf2 MVC, ServiceManager

namespace Westdc\Service;

use Zend\ServiceManager\ServiceManager as Zend_ServiceManager;

class ServiceManager {

private $serviceManager;

function __construct()
{
    $this->serviceManager = new Zend_ServiceManager;
    $this->serviceManager->addAbstractFactory(new ServiceFactory);

    $configService = $this->serviceManager->get('ConfigService');
    $invoked_services = $configService->get('service.invoked.ini');

    foreach($invoked_services as $k=>$v) {
        $this->serviceManager->setInvokableClass($k, $v);
    }
}

public function addKey($key,$value = "")
{
    if(!empty($value))
        $this->serviceManager->$key($value);
    else
        $this->serviceManager->$key();
}

public function setServiceManager(Zend_ServiceManager $service)
{
    $this->serviceManager = $service;
}

public function getServiceManager()
{
    return $this->serviceManager;
}

} 

,

use Westdc\Service\ServiceManager;

$serviceManager = new ServiceManager();
$serviceManager = $serviceManager->getServiceManager();

$authService = $serviceManager->get('Auth');
+3

, 100% , ;)

  • classServiceManager, .
  • classServiceManager ZF2 ServiceManager. , ServiceManager.

, , , - :

$originalSM = new ServiceManager();
$originalSM->setService('csm', 'ZF2\classServiceManager');

$a = $orignalSM->get('csm');

, ServiceManager ZF2 ServiceManager, ServiceManager ZF2, .

. ServiceManager __construct(). SetterInjection, , .

, , .

0

All Articles