Zend Framework 3:无法将服务解析为工厂

Zend Framework 3 : Unable to resolve service as a factory

我已经看到很多关于我的问题的帖子,但是 none 能够修复它..

确实,我为我的 FournitureTable 模型创建了一个工厂,返回的错误是:

Unable to resolve service "Fourniture\Model\FournitureTable" to a factory; are you certain you provided it during configuration?

这是我的模块的主要结构:

... Fourniture
  /config/module.config.php
  /src/Controller/FournitureController.php
      /Factory/FournitureControllerFactory.php
      /Model
         /Factory/FournitureTableFactory.php
         Fourniture.php
         FournitureTable.php
      Module.php # Which only contains getConfig() method


FournitureTable.php

<?php 
    namespace Fourniture\Model;

    use RuntimeException;
    use Zend\Db\TableGateway\TableGatewayInterface;
    use Zend\Db\Sql\Expression;
    use Zend\Db\Sql\Select;

    class FournitureTable {
        private $tableGateway;
        
        public function __construct(TableGatewayInterface $tableGateway){
            $this->tableGateway = $tableGateway;
        }

        public function fetchAll(){
            return $this->tableGateway->select();
        }

        public function getSupplyByCategId($id){
            
            $select = $this->tableGateway->getSql()->select()
            ->join(['sc' => 'sous_categorie'], 'fournitures.categorie = sc.id', ['nom'],
                Select::JOIN_LEFT)
            ->where(['fournitures.categorie' => (int)$id]);
            
            $result = $this->tableGateway->selectWith($select);
            return $result;
        }
    }
?>

FournitureTableFactory.php

<?php

namespace Fourniture\Model\Factory;

use Fourniture\Model\Fourniture;
use Fourniture\Model\FournitureTable;

use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\TableGateway\TableGateway;

use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;

class FournitureTableFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $dbAdapter = $container->get(AdapterInterface::class);
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Fourniture());
        $tableGateway = new TableGateway('fournitures', $dbAdapter, null, $resultSetPrototype);

        return new FournitureTable($tableGateway);
    }
}

module.config.php

<?php
//@NOTE : Se référer aux commentaires de /module/Application/config/module.config.php pour le routage, les contrôleurs et les vues

use Fourniture\Model\FournitureTable;
use Fourniture\Controller\FournitureController;
use Fourniture\Factory\FournitureControllerFactory;
use Fourniture\Model\Factory\FournitureTableFactory;
use Zend\Router\Http\Segment;

return [
    'controllers' => [
        'factories' => [
            Fourniture\Controller\FournitureController::class => Fourniture\Factory\FournitureControllerFactory::class,
            Fourniture\Model\FournitureTable::class => Fourniture\Model\Factory\FournitureTableFactory::class,
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            'fourniture' => __DIR__ . '/../view',
            __DIR__ . '/../view', 
        ],
    ],
];
?>

我是 Zend 3 和 Whosebug 的新手,如果我的解释有点混乱,我深表歉意。

如果我的英语不完美,我也很抱歉,我是法国人。

提前致谢!

您在错误的配置键下声明了工厂。

controllers 顾名思义,配置意味着...控制器。只有 ControllerManager 会查看该配置。

tutorial 中所述,您要查找的是 service_manager
实际上,在教程中他们通过 ConfigProviderInterface 方法检索配置,但是 here 你会找到相应的数组键配置。

最后两个提示:

  • 因为您在文件的开头声明所有 类 (use ....),所以不需要在数组 [=] 中使用完全限定名称 space 37=]
  • 尽量保持结构的一致性。您将 FournitureTableFactory 放在 \Model\Factory 文件夹中,但将 FournitureControllerFactory 放在 Factory 文件夹中。两地两地的工厂,遵循两种不同的逻辑,没有多大意义;)

将您的 module.config.php 更改为:

<?php

use Fourniture\Model\FournitureTable;
use Fourniture\Controller\FournitureController;
use Fourniture\Factory\FournitureControllerFactory;
use Fourniture\Model\Factory\FournitureTableFactory;
use Zend\Router\Http\Segment;

return [
    'controllers' => [
        'factories' => [
            FournitureController::class => FournitureControllerFactory::class
        ],
    ],
    'service_manager' => [
        'factories' => [
            FournitureTable::class => FournitureTableFactory::class
        ]
    ],
    'view_manager' => [
        'template_path_stack' => [
            'fourniture' => __DIR__ . '/../view',
            __DIR__ . '/../view', 
        ],
    ],
];
?>