Zf 3 介绍服务和 ServiceManager

Zf 3 Introducing Services and the ServiceManager

我遇到错误:

Fatal error: Class Blog\Factory\ListControllerFactory contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Zend\ServiceManager\Factory\FactoryInterface::__invoke) in /d0/home/kgendig/www/Zend/module/Blog/src/Blog/Factory/ListControllerFactory.php on line 28

我正在做所有事情 https://framework.zend.com/manual/2.4/en/in-depth-guide/services-and-servicemanager.html

我要改变的,我的 zend_version();是 2.6.0

<?php
// Filename: /module/Blog/src/Blog/Factory/ListControllerFactory.php
namespace Blog\Factory;

use Blog\Controller\ListController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ListControllerFactory implements FactoryInterface
{
    /**
     * Create service
     *
     * @param ServiceLocatorInterface $serviceLocator
     *
     * @return mixed
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $realServiceLocator = $serviceLocator->getServiceLocator();
        $postService        = $realServiceLocator->get('Blog\Service\PostServiceInterface');

        return new ListController($postService);
    }
}

您的 ListControllerFactory class 实现了 FactoryInterface。所以你必须在你的 class 中定义这个接口的所有抽象函数。在这里,您的 FactoryInterface 需要 __invoke() 方法(检查您如何调用它),因此您必须在 ListControllerFactory.

中定义它

您似乎在混合 ZF2/3 成分。在 ZF3 FactoryInterface 代码 (see here) 中,您有从 V2 升级到 V3 的说明:

If upgrading from v2, take the following steps:

  • rename the method createService() to __invoke(), and:
  • rename the $serviceLocator argument to $container, and change the typehint to Interop\Container\ContainerInterface
  • add the $requestedName as a second argument
  • add the optional array $options = null argument as a final argument
  • create a createService() method as defined in this interface, and have it proxy to __invoke().

这描述了如何解决您的问题,但您的代码中可能存在多个类似问题。尽量不要混合使用 ZF2.4 和 ZF3 组件。不要在 composer.json 中使用 dev-master。我建议你只使用 ZF2 组件和 ZF2 教程,或者,如果你想学习 ZF3,只使用 ZF3 组件和 ZF3 教程。