Zend Framework 3:FournitureTable服务无法解析为工厂的问题求助
Hey there! No worries at all about your English or being new to ZF3/Stack Overflow—let's fix that error together. 😊
The Root Cause
You've registered your FournitureTableFactory under the controllers > factories section of your module.config.php, but in Zend Framework 3, service factories (like database table classes) need to be registered in the service_manager configuration key instead. The container is looking for your table's factory in the wrong place, hence the error.
Corrected module.config.php
Update your configuration to move the FournitureTable factory to the service_manager section, like this:
<?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' => [ Fourniture\Controller\FournitureController::class => Fourniture\Factory\FournitureControllerFactory::class, // Removed FournitureTable factory from here! ], ], // Add this service_manager section to register your table factory 'service_manager' => [ 'factories' => [ Fourniture\Model\FournitureTable::class => Fourniture\Model\Factory\FournitureTableFactory::class, ], ], 'view_manager' => [ 'template_path_stack' => [ 'fourniture' => __DIR__ . '/../view', __DIR__ . '/../view', ], ], ]; ?>
Quick Check for Your Controller Factory
Make sure your FournitureControllerFactory correctly retrieves the FournitureTable service from the container. It should look something like this:
<?php namespace Fourniture\Factory; use Fourniture\Controller\FournitureController; use Fourniture\Model\FournitureTable; use Interop\Container\ContainerInterface; use Zend\ServiceManager\Factory\FactoryInterface; class FournitureControllerFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { // Fetch the FournitureTable service from the container $fournitureTable = $container->get(FournitureTable::class); // Pass it to the controller constructor return new FournitureController($fournitureTable); } } ?>
Extra Notes
- Ensure your
Fournituremodel class has all the necessary properties and a matchingexchangeArraymethod (since you're usingResultSetwith anArrayObjectPrototype—this is required to map database rows to your model object). - Double-check all namespace references are correct (your existing code looks good here, but it's easy to mix these up when starting out!).
内容的提问来源于stack exchange,提问作者Rlecomte

