gpt4 book ai didi

php - ZF3 Introducing Blog Module 你确定你在配置期间提供了它吗

转载 作者:行者123 更新时间:2023-12-02 04:35:34 28 4
gpt4 key购买 nike

所以我正在尝试学习 zend framework 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/

教程在“Writing a Factory Class”这一点说这个错误消息是预期的,但在“Registering Services”它应该得到修复,好吧,它没有。

我的 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;
}
}

最佳答案

根据您在 module.config.php 中的代码,类 Factory\ListControllerFactory 应该存在。

'controllers' => [
'factories' => [
// Update the following line:
Controller\ListController::class => Factory\ListControllerFactory::class,
],
],

从错误消息来看,这意味着 Blog\Controller\ListController Controller 未创建。我猜是因为没有工厂类。

您可以像这样创建 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));
}
}

请尝试,我认为这会有所帮助

关于php - ZF3 Introducing Blog Module 你确定你在配置期间提供了它吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43538007/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com