Question

I have a ViewHelper and want to initialize it in Module#getViewHelperConfig():

<?php
namespace Search;

use statements...

class Module implements
    ConfigProviderInterface,
    ServiceProviderInterface,
    AutoloaderProviderInterface,
    ViewHelperProviderInterface {

    public function getConfig() ...

    public function getAutoloaderConfig() ...

    public function getServiceConfig() {
        $breakpoint = null;
        try {
            return array (
                'factories' => array(
                    ...
                    'SearchFormCourseSearchForm' => function ($serviceManager) {
                        $cacheService = $serviceManager->get('Cache\Model\CityStorage');
                        $cities = $cacheService->getCities();
                        $searchForm = new Form\CourseSearchForm($cities);
                        return $searchForm;
                    },
                )
            );
        } ...
    }

    public function getViewHelperConfig() {
        $breakpoint = null;
        return array(
            'factories' => array(
                'searhForm' => function($serviceManager) {
                    $helper = new View\Helper\SearchForm(array('render' => true, 'redirect' => false));
                    $helper->setViewTemplate('search/search/search-courses');
                    $searchForm = $serviceManager->get('SearchFormCourseSearchForm');
                    $helper->setSearchForm($searchForm);
                    return $helper;
                }
            )
        );
    }

But ZF doesn't call my factory. Instead of this it tries to create a new SearchFormCourseSearchForm instance:

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for SearchFormCourseSearchForm' in /var/www/bar/foo/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 456

How should I use service factories got from Module#getServiceConfig() creating a ViewHelper in Module#getViewHelperConfig()?

Was it helpful?

Solution

You need to get the main service locator. The $serviceManager in getViewHelperConfig() is the View Helper service locator.

To get the main service locator in your getViewHelperConfig() function, do the following:

$maimSm = $serviceManager->getServiceLocator();

Therefore:

public function getViewHelperConfig() {
    return array(
        'factories' => array(
            'searhForm' => function($serviceManager) {
                $helper = new View\Helper\SearchForm(array('render' => true, 'redirect' => false));
                $helper->setViewTemplate('search/search/search-courses');
                $maimSm = $serviceManager->getServiceLocator();
                $searchForm = $maimSm->get('SearchFormCourseSearchForm');
                $helper->setSearchForm($searchForm);
                return $helper;
            }
        )
    );
}

There are quite a few posts explaining this strategy in a bit more details, I'll go hunt for them.

Edit

See Using Zend Framework service managers in your application for a nice description of service managers/locators in ZF2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top