Zend Framework 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;
}
}
を必要とするクラスで、[OK]を別の
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);
}
}
に1つの依存関係を注入する工場に必要な、今度は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
部分をスキップすることができますし、コントローラクラスに直接プラグインジェクトが必要な場合
あなただけ遅れて相手に5年:) – tasmaniski
@ tasmaniski 3年前私はZF2を使っていましたが、ZF1よりも遅かったです。その理由から、私はZF1を続けて使用しています:) –