php - 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 的新手,如果我的解释有点混乱,我很抱歉。
如果我的英语不完美也很抱歉,我是法国人。
提前致谢 !
解决方案
您在错误的配置键下声明了工厂。
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',
],
],
];
?>
推荐阅读
- hibernate - 休眠会话的问题
- stripe-payments - 将 GooglePay 与 Stripe 网关结合使用
- python - 使用多个 2d 图像生成 3d 云点
- scala - 计算由窗口函数分区的给定列的不同值,而不使用 approx_count_distinct()
- android-room - Android房间插入/更新结果
- javascript - Vue Bootstrap 加载按钮
- python-3.x - 如何从数据框中的日期列中删除 HH:MM:SS?
- sql - 在 Impala 中查找月份中的第几周
- postgresql - 在 Intellij 中运行 PostgreSQL 存储过程
- vim - Windows 终端应用程序中 Ubuntu 20.04 终端上的 vim 光标位置错误