首页 > 解决方案 > Zend Framework 3:无法将服务解析为工厂

问题描述

我已经看到很多关于我的问题的线程,但没有一个能够解决它..

事实上,我为我的 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);
    }
}

模块.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 和 StackOverflow 的新手,如果我的解释有点混乱,我很抱歉。

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

提前致谢 !

标签: phpzend-frameworkmodel

解决方案


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

controllers顾名思义,配置是指……控制器。只有ControllerManager内部查看该配置。

您正在寻找的是service_manager,如教程中所述。
实际上,在教程中他们通过ConfigProviderInterface方法检索配置,但在这里您会找到相应的数组键配置。

最后两个提示:

  • 由于您在文件 ( ) 的开头声明了所有类use ....,因此无需在数组中使用完全限定的名称空间
  • 尽量保持结构的一致性。你放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', 
        ],
    ],
];
?>

推荐阅读