Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for …

zf-logo-mark

Maybe this is a silly error but still it took me more then 10 minutes to figure it out.

If you are:

  1. using Zend Framework 2
  2. you use the abstract factories to declare how your services should be built
  3. and you get an exception that states “Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for […]”

chances are you are missing a little bit of configuration in your factory.

In my case I was missing the name of the service I wantend to use in theĀ $validNames array, at the beginning of the AbstractFactory.

use Zend\ServiceManager\AbstractFactoryInterface;

class MyAbstractFactory implements AbstractFactoryInterface
{
    protected $validNames = array(
        'servicename1',
        /* 'servicename2' is missing! */
    );

    public function createServiceWithName(ServiceLocatorInterface $sm, $name, $requestedName)
    {
        switch ($requestedName) {
            case 'servicename1':

                return ...;

            case 'servicename2':

            return ...
        }
    }
}

Hope it helps!