In Zend Framework 2/3 können Sie fast jede Klasse in eine andere injizieren. Wenn Sie zum Beispiel das Plugin basePath
benötigen (das im View-Kontext verfügbar ist), können Sie dieses Plugin in Ihre Modell-/Service- oder Controller-Klasse einfügen. Dies ist die empfohlene Art und Weise:
Diese Klasse ist, wo Sie dieses Plugin oder jede andere Dienstleistung
use Zend\View\Helper\BasePath;
class MyService
{
/**
* @var BasePath
*/
protected $plugin;
/**
* MyService constructor.
*
* @param BasePath $basePath
*/
public function __construct(BasePath $basePath)
{
$this->plugin = $basePath;
}
/**
* @return BasePath
*/
public function getPlugin()
{
return $this->plugin;
}
/**
* @param BasePath $plugin
*/
public function setPlugin($plugin)
{
$this->plugin = $plugin;
}
}
Jetzt brauchen, Sie Fabrik benötigen eine Abhängigkeit in einer anderen
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use MyNamespace\Service\MyService;
class MyServiceFactory implements FactoryInterface
{
/**
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return MyService
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$class = $requestedName ? $requestedName : MyService::class;
$plugin = $container->get('ViewHelperManager')->get('BasePath'); // inject this class
$myService = new $class($plugin); // into this class
return $myService;
}
/**
* Provided for backwards compatibility; proxies to __invoke().
*
* @param ContainerInterface|ServiceLocatorInterface $container
* @return MyService
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container, MyService::class);
}
}
Ok zu injizieren, jetzt MyService
hat basePath
Plugin, aber um es in den Controller zu verwenden, müssen Sie Ihren Dienst in den Controller injizieren. So ...
Indexcontroller
use MyNamespace\Service\MyService;
use Zend\Mvc\Controller\AbstractActionController;
class IndexController extends AbstractActionController
{
/**
* @var MyService
*/
protected $service;
/**
* IndexController constructor.
*
* @param MyService $service
*/
public function __construct(MyService $service)
{
$this->service = $service;
}
public function indexAction()
{
$plugin = $this->service->getPlugin(); // Zend\View\Helper\BasePath object
//...
}
}
... und Fabrik für unsere Controller ...
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use MyNamespace\Controller\IndexController;
class IndexControllerFactory implements FactoryInterface
{
/**
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return IndexController
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$class = $requestedName ? $requestedName : IndexController::class;
$myService = $container->getServiceLocator()->get('MyNamespace\Service\MyService');
$controller = new $class($myService);
return $controller;
}
/**
* Provided for backwards compatibility; proxies to __invoke().
*
* @param ContainerInterface|ServiceLocatorInterface $container
* @return IndexController
*/
public function createService(ServiceLocatorInterface $container)
{
return $this($container, IndexController::class);
}
}
Es ist fast fertig. Der letzte Schritt ist Konfiguration in module.config.php
Datei
use MyNamespace\Controller;
use MyNamespace\Factory;
return [
//...
'service_manager' => [
'factories' => [
Service\MyService::class => Factory\Service\MyServiceFactory::class
]
],
'controllers' => [
'factories' => [
Controller\IndexController::class => Factory\Controller\IndexControllerFactory::class
],
],
]
Einfach zu setzen, nicht wahr?
Wenn Sie in Controller-Plugin, aber nicht in Ihrem Modell/Serviceklasse, können Sie MyService
Teil dieser „Übung“ überspringen und injizieren direkt in Controller-Klasse Plugin
Sie nur spät zur Party 5 Jahre :) – tasmaniski
@ tasmaniski Vor 3 Jahren benutzte ich ZF2, aber es war langsamer als ZF1. Aus diesem Grund benutze ich weiterhin ZF1 :) –