Skip to content

Instantly share code, notes, and snippets.

@dmitrybelyakov
Last active August 29, 2015 13:56
Show Gist options
  • Save dmitrybelyakov/9109993 to your computer and use it in GitHub Desktop.
Save dmitrybelyakov/9109993 to your computer and use it in GitHub Desktop.

Looking for a way to define a view helper via configuration. That helper should be created via callback factory and have a dependency injected in constructor, as per documentation here: Registering Helpers

So there we go with a helper itself:

class Locale extends AbstractHelper
{

    protected $localeService;

    public function __construct(LocaleService $localeService = null) {
        $this->localeService = $localeService;
    }

    public function __invoke() {
        return $this->localeService->getCurrentLocale();
    }


} 

An it's being defined in configuration like this:

    //view helpers
    'view_helpers' => array(
        'invokables' => array(),
        'factories' => array(
            'currentLocale' => function($pluginManager) {
                return new \ShiftKernel\Locale\ViewHelper\Locale(
                    $pluginManager->get('ShiftKernel\Locale\LocaleService')
                );

            },
        ),
    ),

Whenever I now use it just explodes with an exception:

Fatal error: Call to undefined method Zend\ServiceManager\ServiceManager::setView() in /_www/hosts/zend2/zend2/vendor/zendframework/zendframework/Zend/View/HelperPluginManager.php on line 135

Am I wrong in how I instantiate helper dependency? Isn't it possible to get() the service from PuginManager (since it's a ServiceManager by itself)? Will I need to define dependency service as factory in plugin managers factories? Is it at all possible to do what I'm trying to achieve?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment