2017-04-21 60 views
1

期間提供這麼即時努力學習Zend框架3,我對這個錯誤現在stucking因爲時間:ZF3介紹博客模塊,你肯定你的配置

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

基於對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)); 
    } 
} 

請嘗試,我認爲,這將有助於

+0

喜多利,首先感謝您的回答和我的遲到一個遺憾。其實,我已經創建了一個ListControllerFactory但有不同的回報: '公共職能__invoke(ContainerInterface $容器,$ requestedName,數組$選項= NULL){ 返回 新ListController($容器 - >獲取(PostRepositoryInterface ::類)); }' – Kden

+0

沒問題,我認爲回報是一樣的。因爲您使用「別名」名稱進行訪問。那麼,你是否仍然遇到問題? –

+0

是的,仍然是同樣的問題。 – Kden