gpt4 book ai didi

php - 在没有 getServicelocator() 的情况下从 ZF2 迁移到 ZF 3;

转载 作者:搜寻专家 更新时间:2023-10-31 21:21:14 24 4
gpt4 key购买 nike

我是 ZF3 的新人,我需要你的帮助。ZF3 中不再有服务定位器。所以我创建了一个工厂类来替换我代码中的服务定位器。

这是我的 AbstractController.php 文件中带有服务定位器的代码:

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 中,您要做的第一件事就是为它创建服务和工厂。让我们以服务文件夹中的 UserManager.php 为例。

所以我们在文件夹 Service -> UserManager.phpService -> 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,
],
],

基本上就是这样,我们可以在我们的 Controller 中注入(inject)服务并完成工作:

<?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 中使用服务。

祝你好运!

关于php - 在没有 getServicelocator() 的情况下从 ZF2 迁移到 ZF 3;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47875015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com