[英]Migration from ZF2 to ZF 3 without getServicelocator();
我是ZF3的新手,我需要你的帮助。 在ZF3中,没有服务定位器了。 所以我创建了一个Factory类来替换我的代码中的服务定位器。
这是我的AbstractController.php文件中包含服务Locator的代码:
protected function getService()
{
return $this->getServiceLocator()->get($service); //remove from ZF3
return $this->service = $service;
}
现在我替换了AbstractController.php中的服务定位器:
protected function getService($service)
{
$service = $this->service;
return $this->service = $service;
}
在Module.Config.php中,我添加了以下行:
return [
'controllers' => [
'factories' => [
Controller\AbstactController::class => Controller\AbstactControllerFactory::class,
],
],
我创建了一个AbstractControllerFactory文件,其中包含以下行:
<?php
namespace Application\Controller;
use Application\Controller\AbstractController;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
class AbstractControllerFactory implements FactoryInterface
{
protected function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new AbstractController($container->get(service::class));
}
}
我需要知道这是否是从ZF2到ZF3的正确迁移?
在ZF3中,您要做的第一件事就是为它创建服务和工厂。 让我们在Services文件夹中使用此示例UserManager.php。
所以我们在文件夹Service - > UserManager.php和Service - > Factory - > UserManagerFactory.php中
UserManagerFactory.php:
<?php
namespace User\Service\Factory;
use Interop\Container\ContainerInterface;
use User\Service\UserManager;
/**
* This is the factory class for UserManager service. The purpose of the factory
* is to instantiate the service and pass it dependencies (inject dependencies).
*/
class UserManagerFactory
{
/**
* This method creates the UserManager service and returns its instance.
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$entityManager = $container->get('doctrine.entitymanager.orm_default');
return new UserManager($entityManager);
}
}
UserManager.php:
<?php
namespace User\Service;
use User\Entity\User;
/**
* This service is responsible for adding/editing users
* and changing user password.
*/
class UserManager
{
/**
* Doctrine entity manager.
* @var Doctrine\ORM\EntityManager
*/
private $entityManager;
/**
* Constructs the service.
*/
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
// REST OF YOUR CODE
}
现在我们有了服务,我们进入User \\ config \\ modules.config.php :
'service_manager' => [
'factories' => [
Service\UserManager::class => Service\Factory\UserManagerFactory::class,
],
],
那基本上就是这样,我们可以在我们的控制器中注入服务并完成工作:
<?php
namespace User\Controller;
use Zend\Mvc\Controller\AbstractActionController;
/**
* This controller is responsible for user management (adding, editing,
* viewing users and changing user's password).
*/
class UserController extends AbstractActionController
{
/**
* User manager.
* @var User\Service\UserManager
*/
private $userManager;
/**
* Constructor.
*/
public function __construct($userManager)
{
$this->userManager = $userManager;
}
// REST OF YOUR CODE
}
我真的希望这有助于您了解如何在ZF3中使用Service。
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.