Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceNotFoundException in ZendFramework 2, example from Rob Allen

for the past few weeks I have been following ZF2 especially Rob Allen's 'Album' example , I have created the example DB-'zf2tutorial' and example table-'album', which works fine fetching all the items when I use php-mysql, so problems with the data in the DB.

My local.php looks like this config.autoload/local.php:

return array(
    'db' => array
    (
        'driver'         => "Pdo",
        'dsn'            => "mysql:dbname=zf2tutorial;hostname=localhost",
        'username'       => "user", //here I added my valid username 
        'password'       => "password", //here I added my valid password 
        'driver_options' => array
        (
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
        ),
    ),
);

Module.php **module/Album/Model.php

<?php
namespace Album;

use Album\Model\AlbumTable;

class Module 
{
    public function getAutoloaderConfig() 
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig() 
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfiguration() 
    {
        return array(

            'factories' => array(

                'album-table' => function($sm) //also tried this 'Album\Model\AlbumTable' =>  function($sm)
                {
                    $dbAdapter = $sm->get('db-adapter');//also tried this $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new AlbumTable($dbAdapter);
                    return $table;
                },
            ),
        );
    }  
}

I just wanted to check whether the zf2turorial/album works or not it does throw this error which is similar to this post here in stackoverlow.

The error which it is throwing is: Additional information: Zend\ServiceManager\Exception\ServiceNotFoundException

File:
..\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:392
Message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Album\Model\AlbumTable

I have followed ZF2 Beta 5 tutorial as well, but still encountering this problem. In case if anyone has a solution, please do share with us.

Thanks

like image 650
125369 Avatar asked Jul 27 '12 12:07

125369


4 Answers

seems like somebody has forgotten to update the newest changes in zf2.

the solution:

the file module/Album/Module.php has to contain this content:

<?php

namespace Album;

use Album\Model\AlbumTable;
use Zend\ModuleManager\Feature\ServiceProviderInterface;

class Module implements ServiceProviderInterface

then you have to rename

public function getServiceConfig()

to

public function getServiceConfiguration()
like image 147
rastusik Avatar answered Oct 01 '22 00:10

rastusik


public function getServiceConfiguration() is not the correct function name

rename getServiceConfiguration() to getServiceConfig()

although it is not mandatory you should declare the class Module as

class Module implements ServiceProviderInterface
like image 28
Paulo Avatar answered Oct 01 '22 00:10

Paulo


I am not sure but try to put the code into

config.autoload/global.php: like this way

return array(
    'db' => array(

        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=kd_tutorial;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);
like image 37
Kdecom Avatar answered Oct 01 '22 00:10

Kdecom


I have encoutered the same problem when try to implement code follow zend tutorial. The main reason is order-folder

module
    ModuleX
        config
        src
            ModuleX //may be you forget this one
                Controller
                Factory
                Model
                ...
        view
        Module.php

in Module.php, getConfig(), getAutoloaderConfig() is enough to run

class Module implements AutoloaderProviderInterface, ConfigProviderInterface{
public function getAutoloaderConfig(){
    return array(
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
        ),
    );
}

public function getConfig(){
    return include __DIR__ . '/config/module.config.php';
}
like image 33
Anh Le Hoang Avatar answered Oct 01 '22 01:10

Anh Le Hoang