Ich bin wirklich verwirrt wann getServiceLocator verwenden und wann nicht. Als Beispiel:ZF2 wenn getServiceLocator() zu verwenden ist und wenn nicht
+ Module
-+ Helloworld
--+ src
---+ Controller
----+ IndexController.php
----+ IndexControllerFactory.php
---+ Service
----+ LogginService.php
----+ GreetingService.php
----+ GreetingServiceFactory.php
GreetingServiceFactory.php hat den Inhalt:
<?php
namespace Helloworld\Service;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class GreetingServiceFactory implements FactoryInterface
{
public function createService (ServiceLocatorInterface $serviceLocator)
{
$greetingService = new GreetingService();
$greetingService->setEventManager($serviceLocator->get('eventManager'));
$loggingService = $serviceLocator->get('loggingService');
$greetingService->getEventManager()->attach('getGreeting', array(
$loggingService,
'onGetGreeting'
));
return $greetingService;
}
}
Und IndexControllerFactory.php hat den Inhalt:
<?php
namespace Helloworld\Controller;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class IndexControllerFactory implements FactoryInterface
{
public function createService (ServiceLocatorInterface $serviceLocator)
{
$ctr = new IndexController();
$ctr->setGreetingService($serviceLocator->getServiceLocator()
->get('greetingService'));
return $ctr;
}
}
Wie Sie sehen können, ich brauche $ Servicelocator -> getServiceLocator() in meiner ControllerFactory, aber nicht in meiner ServiceFactory. Warum? Beide verwenden dieselbe Schnittstelle ServiceLocatorInterface, die nicht einmal die Methode getServiceLocator() definiert.
module.config.php:
'controllers' => array(
'factories' => array(
'Helloworld\Controller\Index' => 'Helloworld\Controller\IndexControllerFactory'
)
)
,
'service_manager' => array(
'invokables' => array(
'loggingService' => 'Helloworld\Service\LoggingService'
),
'factories' => array(
'greetingService'=> 'Helloworld\Service\GreetingServiceFactory'
),
)
ich jede Klärung schätzen würde :)
einen schönen Tag!
Mit welchem Werkzeug haben Sie dieses Diagramm erstellt? Sieht gut aus. – Leven
@Leven das ist YUML – Ocramius