在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;
}
}
现在,你需要到工厂注入一个依赖到另一个
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
一部分,并直接注入到插件控制器类
你只迟到了5年:) – tasmaniski
@ tasmaniski 3年前我使用ZF2,但它比ZF1慢。出于这个原因,我继续使用ZF1 :) –