【问题标题】:Service Not Created Exception: ZF2服务未创建异常:ZF2
【发布时间】:2015-08-03 01:41:26
【问题描述】:

我正在为初学者完成 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
 }

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

【问题讨论】:

  • 这个错误是因为ZF2无法创建你的控制器的实例,所以调查你的控制器工厂。你确定$postService = $realServiceLocator-&gt;get('Blog\Service\PostServiceInterface'); 有效吗?
  • 我有同样的问题,发现错误发生在 Svengali 询问的行中: $postService = $realServiceLocator->get('Blog\Service\PostServiceInterface');但不能说到底是什么
  • (不仅针对这种情况)如果 createService() 函数中的任何位置或此函数中调用的函数中的任何位置发生任何错误,这将返回(例如:$options = new MyOptions( $paramsConfig); ) 将导致返回此消息。因此,您最好逐行导航以找出问题的确切位置。

标签: php zend-framework zend-framework2


【解决方案1】:

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

教程只说:

正如您从前面的章节中知道的,每当我们需要 参数我们需要为该类编写一个工厂。来吧 为我们的映射器实现创建一个工厂。

你需要为 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'));
    }
}

【讨论】:

  • 谢谢。是的,这个答案肯定有帮助! :)
【解决方案2】:
**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',
    )

【讨论】:

  • 请在您的答案中多写一些 cmets,说明您认为有什么问题以及为什么这应该有效
【解决方案3】:

@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' => '博客\服务\工厂\PostServiceFactory',

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

'Blog\Service\PostServiceInterface' => '博客\工厂\PostServiceFactory',

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-01
    • 2011-11-29
    • 2011-08-31
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多