Вопрос

I started learning ZF2 but have a problem with ServiceManager.

I tried to add new Module "Menu" to ServiceManager.

In my application/config/module.config.php added this code:

'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'menu'       => 'Application\Menu\MenuServiceFactory',
        ),
    ),

    'menu' => array(

    ),

Application\src\Menu\MenuServiceFactory.php:

namespace Application\Menu;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Menu
 */
class MenuServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        // Configure the Menu
        $config = $serviceLocator->get('Config');
        $menuConfig = isset($config['menu']) ? $config['menu'] : array();
        $menu = Menu::factory($menuConfig);
        return $menu;
    }
}

Application\src\Menu\Menu.php:

namespace Application\Menu;


/**
 * Menu
 */
class Menu
{

    /**
     * Instantiate a menu
     *
     * @param  array $options
     * @return Menu
     */
    public static function factory($options)
    {

        $menu = 'test';
        return $menu;
    }


}

If I tried add this "$menu = $this->getServiceLocator()->get('menu');" I received:

    Zend\ServiceManager\Exception\ServiceNotCreatedException

File:
E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:871
Message:
While attempting to create menu(alias: menu) an invalid factory was registered for this instance type.
Stack trace:
#0 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(494): Zend\ServiceManager\ServiceManager->createFromFactory('menu', 'menu')
#1 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php(441): Zend\ServiceManager\ServiceManager->create(Array)
#2 E:\Projects\zf2\module\AdminSettings\src\AdminSettings\Controller\SettingsController.php(193): Zend\ServiceManager\ServiceManager->get('menu')
#3 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): AdminSettings\Controller\SettingsController->logoAction()
#4 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#5 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(472): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#6 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#8 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#9 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#10 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(472): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#11 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 E:\Projects\zf2\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(294): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 E:\Projects\zf2\public\index.php(13): Zend\Mvc\Application->run()
#14 {main}

Does anyone have an idea to resolve this error?

Это было полезно?

Решение

The path for your MenuServiceFactory.php file doesn't match the namespace you've defined in the service manager config.

You need to move your Application/src/Menu folder and all the files it contains to Application/src/Application/Menu

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top