未创建服务异常:ZF2

罗伯特·杨

我正在为初学者完成ZF2深入教程,并且在重新加载页面时,我继续收到以下错误消息:

“执行期间发生错误;请稍后重试。

附加信息:

Zend \ ServiceManager \ Exception \ ServiceNotCreatedException文件:C:\ xampp \ htdocs \ zend \ vendor \ zendframework \ zendframework \ library \ Zend \ ServiceManager \ ServiceManager.php:946消息:创建“ Blog \ Controller \ List”时引发了异常; 没有实例返回”。

在本教程中,我已经准备好数据库,添加了映射器实现,并在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();
    }

}

由于最底行的代码中包含转储命令,因此结果变量的数据转储应返回以下内容:

    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
 }

但是相反,我得到了如上所述的错误页面。

猕猴桃榨汁机

我发现了问题。本教程缺少这一部分。

本教程只说:

从前面的章节中可以知道,只要我们有必需的参数,就需要为该类编写一个工厂。继续并为我们的mapper实现创建工厂。

您需要为ZendDbSqlMapper创建一个工厂:Create /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'));
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章