ZfcTwig is not loading config and fails in bootstrap

Hi I am relatively new to ZF2 so this might be a very simple mistake I am making.

Problem

When loading the ZfcTwig module I get an exception that

Fatal error: Uncaught exception 'ZendServiceManagerExceptionServiceNotFoundException' with message 'ZendServiceManagerServiceManager::get was unable to fetch or create an instance for Twig_Environment' in /www/ZendFramework2/library/Zend/ServiceManager/ServiceManager.php on line 555

This Exception is thrown in the onBootstrap function of ZfcTwigModule.php :

<?php
class Module implements
  BootstrapListenerInterface,
  ConfigProviderInterface
{
  public function onBootstrap(EventInterface $e)
  { 
    /** @var ZendMvcMvcEvent $e*/
    $application    = $e->getApplication();
    $serviceManager = $application->getServiceManager();
    $environment    = $serviceManager->get('Twig_Environment'); // throws the exception
    //...
  }
  public function getConfig(){ return [/*...*/]; } // never called
}

What I don't get is why is bootstrap called before configuration was loaded. The 'Twig_Environment' service is configured in the config of the ZfcTwig module, but that config has not been loaded, when onBootstrap is called.

Setup

ZF2 loader via ZF2_PATH environment variable. Not using the composer autoloader.

in application.config.php I set an additional modules path '/global/vendor' to my sytem wide repository of reusable modules. I am not using a project local vendor folder.

From '/global/vendor/ZfcTwig' I am loading the module ZfcTwig (link) to get Twig template engine support in ZF2.

Since this relies on the twig library I put the twig lib into '/global/vendor/twig'

To enable autoloading for the ZfcTwig module and the twig library classes I changed Module.php of ZfcTwig by implementing the AutoloaderProviderInterface and adding configs for both twig and ZfcTwig.

<?php
class Module implements
    BootstrapListenerInterface,
    AutoloaderProviderInterface,
    ConfigProviderInterface
{
    /**
     * Autoloading the twig library and this modules classes
     * @return array
     */
    public function getAutoloaderConfig()
    {
        $pathToTwigLib = dirname(dirname(dirname(__DIR__))) . '/twig/twig/lib/Twig';
        return array(
            'ZendLoaderStandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__,
                ),
                'prefixes' => array(
                    'Twig_' => $pathToTwigLib,
                ),
            ),
        );
    }

In application.config.php I am loading the modules ['Application','twig','ZfcTwig']

Auto loading for twig is working (at least I can instantiate Twig_Environment in the bootstrap of ZfcTwig and other Controllers using new ).


Had the config cache enabled

The problem is exactly where I was wondering myself. The config needs to be loaded before the bootstrapping is done. This is however exactly what ZF2 does, unless, like me, you have the config cache enabled.

<?php
// config/applicaiton.config.php
return array(
  'module_listener_options' => array(
    // this prevents the call to getConfig on Modules
    // that implement the ConfigProviderInterface
    // default: false
    'config_cache_enabled' => true, 
  )
);
链接地址: http://www.djcxy.com/p/67782.html

上一篇: 结构数组的初始化列表行为

下一篇: ZfcTwig不加载配置并在启动时失败