【问题标题】:Zend Framework 3 : Unable to resolve service as a factoryZend Framework 3:无法将服务解析为工厂
【发布时间】:2021-01-19 13:42:31
【问题描述】:

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

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

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

提前致谢!

【问题讨论】:

    标签: php zend-framework model


    【解决方案1】:

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

    controllers 配置,顾名思义,用于...控制器。只有 ControllerManager 会查看该配置的内部。

    您正在寻找的是service_manager,如tutorial 中所述。
    其实教程中他们通过ConfigProviderInterface方法获取配置,但是here你会找到对应的数组键配置。

    最后两个提示:

    • 由于您在文件开头 (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', 
            ],
        ],
    ];
    ?>
    

    【讨论】:

    • 非常感谢,确实有效!我会牢记您的以下建议:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 2022-08-20
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多