服务未创建例外:ZF2

时间:2015-05-21 11:49:11

标签: php zend-framework zend-framework2

我正在为初学者完成ZF2 In-Depth教程,当我重新加载页面时,我一直收到以下错误消息:

"执行期间发生错误;请稍后再试。

其他信息:

的Zend \的ServiceManager \异常\ ServiceNotCreatedException 文件:C:\ XAMPP \ htdocs中\ Zend的\供应商\ zendframework \ zendframework \库\ Zend的\的ServiceManager \ ServiceManager.php:946 消息:创建" Blog \ Controller \ List"时出现异常;没有实例返回"。

我已经到了教程的阶段,我准备了数据库,添加了mapper实现,并在module.config.php文件中修改了我们的控制器管理器,以便它支持工厂。我似乎无法找出问题所在。我的代码摘录如下:

module.config.php:

  // Tells our module where to find the view files. Can also overwrite view files for other modules.
  'view_manager' => array(
     'template_path_stack' => array(
         __DIR__ . '/../view'
         ),
     ),
           // Tells our module where to find the controller named Blog\Controller\List

  'controllers' => array(
     'factories' => array(
        'Blog\Controller\List' => 'Blog\Factory\ListControllerFactory'
         )
     ),

ListController.php:

    <?php
 // Filename: /module/Blog/src/Blog/Controller/ListController.php
 namespace Blog\Controller;

 use Blog\Service\PostServiceInterface;
 use Zend\Mvc\Controller\AbstractActionController;

 class ListController extends AbstractActionController
 {
     /**
      * @var \Blog\Service\PostServiceInterface
      */
     protected $postService;

     public function __construct(PostServiceInterface $postService)
     {
         $this->postService = $postService;
     }

    public function indexAction()
    {
        return new ViewModel(array(
            'posts' => $this->postService->findAllPosts()
            ));
    }

}

?>

ListControllerFactory.php

    <?php

namespace Blog\Factory;

use Blog\Controller\ListController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ListControllerFactory implements FactoryInterface
{
     /**
      * Create service
      *
      * @param ServiceLocatorInterface $serviceLocator
      *
      * @return mixed
      */

     public function createService(ServiceLocatorInterface $serviceLocator)
     {
        $realServiceLocator = $serviceLocator->getServiceLocator();
        $postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');

          return new ListController($postService);
     }
}

ZendDBSQLMapper.php - 设计为执行对现有数据库的调用的映射器类:

<?php
// Filename: /module/Blog/src/Blog/Mapper/ZendDbSqlMapper.php


namespace Blog\Mapper;

use Blog\Model\PostInterface;
use Zend\Db\Adapter\AdapterInterface;

class ZendDbSqlMapper implements PostMapperInterface
{
    /**
        * @var \Zend\Db\Adapter\AdapterInterface
    */

  protected $dbAdapter;

   /**
  * @param AdapterInterface  $dbAdapter
  */

   public function __constrcut(AdapterInterface $dbAdapter)
   {
        $this->dbAdapter = $dbAdapter;
   }

    /**
  * @param int|string $id
  *
  * @return PostInterface
  * @throws \InvalidArgumentException
  */

    public function find($id)
    {

    }

    /**
  * @return array|PostInterface[]
  */

    public function findAll()
    {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select('posts');

        $stmt = $sql->prepareStatementForSqlObject($select);
        $result = $stmt->execute();

        \Zend\Debug\Debug::dump($result);die();
    }

}

由于底部代码中存在dump命令,结果变量的数据转储应返回如下内容:

    object(Zend\Db\Adapter\Driver\Pdo\Result)#303 (8) {
   ["statementMode":protected] => string(7) "forward"
   ["resource":protected] => object(PDOStatement)#296 (1) {
     ["queryString"] => string(29) "SELECT `posts`.* FROM `posts`"
   }
   ["options":protected] => NULL
   ["currentComplete":protected] => bool(false)
   ["currentData":protected] => NULL
   ["position":protected] => int(-1)
   ["generatedValue":protected] => string(1) "0"
   ["rowCount":protected] => NULL
 }

但我得到了如上所述的错误页面。

3 个答案:

答案 0 :(得分:2)

我发现了这个问题。该教程缺少此部分。

教程只说:

  

正如您在前面的章节中所知,每当我们有必要时   参数我们需要为类编写工厂。来吧   为我们的mapper实现创建一个工厂。

您需要为ZendDbSqlMapper创建一个工厂: 创建/Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php

<?php
// Filename: /Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php
namespace Blog\Factory;

use Blog\Mapper\ZendDbSqlMapper;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ZendDbSqlMapperFactory implements FactoryInterface
{
    /**
    * Create service
    *
    * @param ServiceLocatorInterface $serviceLocator
    *
    * @return mixed
    */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new ZendDbSqlMapper($serviceLocator->get('Zend\Db\Adapter\Adapter'));
    }
}

答案 1 :(得分:0)

**and also use this one :**

'service_manager' => array(
    'factories' => array(
        'Blog\Mapper\PostMapperInterface'   => 'Blog\Factory\ZendDbSqlMapperFactory',
        'Blog\Service\PostServiceInterface' => 'Blog\Factory\PostServiceFactory',
        'Zend\Db\Adapter\Adapter'           => 'Zend\Db\Adapter\AdapterServiceFactory',
    )

答案 2 :(得分:0)

@kiwi Juicer首先指出教程有两个问题。缺少ZendDbSqlMapperFactory.php。

创建文件:/Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php并填写以下内容:

    <?php// Filename: /module/Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php

namespace Blog\Factory;

 use Blog\Mapper\ZendDbSqlMapper;
 use Blog\Model\Post;
 use Zend\ServiceManager\FactoryInterface;
 use Zend\ServiceManager\ServiceLocatorInterface;
 use Zend\Stdlib\Hydrator\ClassMethods;

 class ZendDbSqlMapperFactory implements FactoryInterface
 {
     /**
      * Create service
      *
      * @param ServiceLocatorInterface $serviceLocator
      *
      * @return mixed
      */
     public function createService(ServiceLocatorInterface $serviceLocator)
     {
         return new ZendDbSqlMapper(
             $serviceLocator->get('Zend\Db\Adapter\Adapter'),
             new ClassMethods(false),
             new Post()
         );
     }
 }

然后打开模块/ Blog / conf / blog.conf.php

在原始的blog.conf.php中,他们分配了PostServiceFactory的无效路径存在问题

  

'Blog \ Service \ PostServiceInterface'=&gt;   '博客\服务\厂\ PostServiceFactory',

使用正确的路径查找并替换上面的行:

  

'Blog \ Service \ PostServiceInterface'=&gt;   '博客\厂\ PostServiceFactory',