Service Not Created Exception: ZF2

I am completing the ZF2 In-Depth tutorial for beginners, and I keep on receiving the following error message when I reload the page:

"An error occurred during execution; please try again later.

Additional information:

Zend\ServiceManager\Exception\ServiceNotCreatedException File:C:\xampp\htdocs\zend\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:946 Message:An exception was raised while creating "Blog\Controller\List"; no instance returned".

I have reached the stage in the tutorial where I have prepared the database, added the mapper implementation, and altered our controller manager in the module.config.php file so that it supports factories. I cannot seem to spot where the problem lies. An extract of my code is below:

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 - Designed as the mapper class to perform calls to the existing database:

<?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();
    }

}

As there is a dump command in the code at the bottom line, the data dumping of the result variable should return something like:

    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
 }

But instead I get the error page as described above.

Answers


I found the issue. The tutorial is missing this part.

The tutorial only says:

As you know from previous chapters, whenever we have a required parameter we need to write a factory for the class. Go ahead and create a factory for our mapper implementation.

You need to create a factory for the 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'));
    }
}

**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',
    )

There are two problems with the tutorial first is point out by @kiwi Juicer.. ZendDbSqlMapperFactory.php is missing.

Create File: /Blog/src/Blog/Factory/ZendDbSqlMapperFactory.php and fill the following content:

    <?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()
         );
     }
 }

Then open module/Blog/conf/blog.conf.php

In the original blog.conf.php there is problem they are assigning invalid path of PostServiceFactory

'Blog\Service\PostServiceInterface' => 'Blog\Service\Factory\PostServiceFactory',

find and replace above line with correct path:

'Blog\Service\PostServiceInterface' => 'Blog\Factory\PostServiceFactory',


Need Your Help

Pyglet 1.2alpha1 not working on Python 3.3.0

python-3.x pyglet

I can't use Pyglet 1.2alpha1 (which should be able to run under Py3) from Python 3.

How to integrate a facebook app in a facebook group?

facebook-apps

Is it possible to integrate functionalities from a facebook app into a facebook group? For example a karma system? Are there any APIs for that?