没有getServicelocator()从ZF2迁移到ZF 3;

时间:2017-12-18 19:16:58

标签: php zend-framework2 migration zend-framework3

我是ZF3的新手,我需要你的帮助。 在ZF3中,没有服务定位器了。所以我创建了一个Factory类来替换我的代码中的服务定位器。

以下是我在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的正确迁移?

1 个答案:

答案 0 :(得分:1)

在ZF3中,您要做的第一件事就是为它创建服务和工厂。让我们在服务文件夹中接受这个示例UserManager.php。

所以我们在文件夹服务 - &gt; UserManager.php 以及服务 - &gt; 工厂 - &gt;的 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。

祝你好运!