所以即时通讯尝试学习zend框架3,我现在因为几个小时而错过了这个错误:
Unable to resolve service "Blog\Controller\ListController" to a factory; are
you certain you provided it during configuration?
我正在学习的教程: https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/
教程在“编写工厂类”这一点上说这个错误信息是预期的,但是在“注册服务”它应该得到修复,好吧,它没有。
我的module.config.php:
<?php
namespace Blog;
use Zend\Config\Factory;
use Zend\Router\Http\Literal;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'service_manager' => [
'aliases' => [
Model\PostRepositoryInterface::class => Model\PostRepository::class,
],
'factories' => [
Model\PostRepository::class => InvokableFactory::class,
],
],
'controllers' => [
'factories' => [
// Update the following line:
Controller\ListController::class =>
Factory\ListControllerFactory::class,
],
],
'router' => [
'routes' => [
'blog' => [
'type' => Literal::class,
'options' => [
'route' => '/blog',
'defaults' => [
'controller' => Controller\ListController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
我的ListController.php:
namespace Blog\Controller;
use Blog\Model\PostRepositoryInterface;
use Zend\Mvc\Controller\AbstractActionController;
class ListController extends AbstractActionController
{
/**
* @var PostRepositoryInterface
*/
private $postRepository;
public function __construct(PostRepositoryInterface $postRepository)
{
$this->postRepository = $postRepository;
}
}
答案 0 :(得分:0)
根据module.config.php
中的代码,类Factory\ListControllerFactory
应该存在。
'controllers' => [
'factories' => [
// Update the following line:
Controller\ListController::class => Factory\ListControllerFactory::class,
],
],
从错误消息中,它表示未创建Blog\Controller\ListController
控制器。我想因为没有工厂级别。
您可以像这样创建Blog\Controller\ListControllerFactory
类,
<?php
namespace Blog\Controller;
use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
class ListControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new ListController($container->get(Blog\Model\PostRepository::class));
}
}
请尝试,我认为这会有所帮助
答案 1 :(得分:0)
请删除该行:
use Zend\Config\Factory;
Factory 名称空间与此类冲突。