【问题标题】:Zend 3 Factory InterfaceZend 3 工厂接口
【发布时间】:2017-04-10 03:01:31
【问题描述】:

我正在尝试为我的控制器实现工厂:

class NumberControllerFactory implements FactoryInterface{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
    return new NumberController($container->get(Bar::class));
}

public function createService(ServiceLocatorInterface $services)
{
    return $this($services, NumberController::class);
}

}

我收到错误:

Fatal error: Declaration of Number\Factory\NumberControllerFactory::__invoke() must be compatible with Zend\ServiceManager\Factory\FactoryInterface::__invoke(Interop\Container\ContainerInterface $container, $requestedName, array $options = NULL) in C:\xampp\htdocs\MyProject\module\Number\src\Number\Factory\NumberControllerFactory.php on line 10

我需要这个,因为我想将模型注入控制器,因为服务管理器已从 Zend 3 中的控制器中删除。

我使用了https://framework.zend.com/manual/2.4/en/ref/installation.html中描述的骨架

在 composer.json 中是:

    "require": {
    "php": "^5.6 || ^7.0",
    "zendframework/zend-component-installer": "^1.0 || ^0.3 || ^1.0.0-dev@dev",
    "zendframework/zend-mvc": "^3.0.1",
    "zfcampus/zf-development-mode": "^3.0"
},

这个问题我没看懂,看了很多教程,比如:

https://zendframework.github.io/zend-servicemanager/migration/

你能帮帮我吗?

我猜目前这个方法是兼容 Zend\ServiceManager\Factory\FactoryInterface::__invoke

【问题讨论】:

    标签: php zend-framework dependencies factory zend-framework3


    【解决方案1】:

    谢谢阿扎尔。

    我的问题是因为当我使用:

             'factories' => array(
            \Number\Controller\NumberController::class => \Number\Factory\NumberControllerFactory::class
         )
    

    它不起作用,有 404...我不得不使用:

    'Number\Controller\Number' => \Number\Factory\NumberControllerFactory::class
    

    在文档中我应该使用完整的类名 ::class。

    有人知道为什么它不起作用吗?

    【讨论】:

    • 您可以使用下面的代码 'Number\Controller\Number' => Factory\NumberControllerFactory::class 或 Controller\NumberController::class=> Factory\NumberControllerFactory::class
    【解决方案2】:

    为了将模型注入控制器,您需要在module.config.php中配置时创建一个工厂类,如下所示

     'controllers' => [
        'factories' => [
            Controller\AlbumController::class => Factory\AlbumControllerFactory::class,
        ],
     ],
    

    这里的 AlbumController 是 Album 模块的控制器类。之后,您需要在 module\Album\src\Factory 中创建一个 AlbumControllerFactory 类。 在这个类中你需要编写如下代码:

      namespace Album\Factory;
    
      use Album\Controller\AlbumController;
      use Album\Model\AlbumTable;
      use Interop\Container\ContainerInterface;
      use Zend\ServiceManager\Factory\FactoryInterface;
    
    
      class AlbumControllerFactory implements FactoryInterface
      {
         public function __invoke(ContainerInterface $container,      $requestedName, array $options = null)
         {
          return new AlbumController($container->get(AlbumTable::class));
         }
      }
    

    您需要在控制器类(AlbumController)中编写以下代码。

       public function __construct(AlbumTable $album) {
         $this->table = $album;
       }
    

    这样你就可以将模型类注入到控制器类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2014-03-21
      • 1970-01-01
      相关资源
      最近更新 更多