젠드 프레임 워크 2/3에서는 거의 모든 클래스를 다른 클래스에 삽입 할 수 있습니다. 예를 들어 basePath
플러그인 (보기 컨텍스트에서 사용 가능)이 필요한 경우이 플러그인을 모델/서비스 또는 컨트롤러 클래스에 삽입 할 수 있습니다. 이것은 권장되는 방법입니다
이이 플러그인 또는
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;
}
}
지금, 다른
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);
}
}
확인에 하나 명의 의존성을 주입하기 위해 공장에 필요한 다른 서비스를 필요로 클래스, 현재 MyService
에는 basePath
플러그인이 있지만 컨트롤러에 사용하려면 컨트롤러에 서비스를 주입해야합니다. 그래서 ...
인 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
//...
}
}
... 그리고 우리의 컨트롤러 공장 ...
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);
}
}
그것은 거의 이루어집니다. 마지막 단계는이 module.config.php
파일
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
],
],
]
쉬운 구성을 설정하는가요?
당신이 컨트롤러 플러그인,하지만 모델/서비스 클래스에서,이 "튜토리얼"의 MyService
부분을 건너 뛸 수 있습니다 및 컨트롤러 클래스에 직접 플러그인 주입이 필요한 경우
당신 늦게 파티에 오년 : – tasmaniski
@ tasmaniski 3 년 전 ZF2를 사용했지만 ZF1보다 느렸다. 그 이유로 저는 ZF1을 계속 사용했습니다. :) –