ZfcTwig没有加载配置并且在bootstrap

时间:2015-08-05 10:36:52

标签: php zend-framework2

嗨,我对ZF2比较陌生,所以这可能是我犯的一个非常简单的错误。

问题

加载ZfcTwig模块时,我得到一个例外

  

致命错误:未捕获的异常' Zend \ ServiceManager \ Exception \ ServiceNotFoundException'消息' Zend \ ServiceManager \ ServiceManager :: get无法为Twig_Environment获取或创建实例'在/www/ZendFramework2/library/Zend/ServiceManager/ServiceManager.php第555行

onBootstrap函数ZfcTwig\Module.php抛出此异常:

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

我没有得到的是为什么在加载配置之前调用bootstrap。 'Twig_Environment'服务在ZfcTwig模块的配置中配置,但是当调用onBootstrap时,该配置尚未加载。

设置

通过ZF2_PATH环境变量加载ZF2。不使用composer自动加载器。

application.config.php中的

我将一个额外的模块路径'/global/vendor'设置到我的可重用模块的系统库中。我没有使用项目本地供应商文件夹。

'/global/vendor/ZfcTwig'我正在加载模块ZfcTwiglink)以获得ZF2中的Twig模板引擎支持。

由于这依赖于twig库,我将twig lib放入其中 '/global/vendor/twig'

要为ZfcTwig模块和twig库类启用自动加载,我通过实现AutoloaderProviderInterface并为twig和ZfcTwig添加配置来更改ZfcTwig的Module.php。

<?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(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__,
                ),
                'prefixes' => array(
                    'Twig_' => $pathToTwigLib,
                ),
            ),
        );
    }

application.config.php我正在加载模块['Application','twig','ZfcTwig']

twig的自动加载正在运行(至少我可以使用Twig_Environment在ZfcTwig和其他控制器的引导程序中实例化new

1 个答案:

答案 0 :(得分:0)

启用了配置缓存

问题正是我想知道自己的问题。需要在引导完成之前加载配置。这正是ZF2所做的,除非和我一样,你启用了配置缓存。

<?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, 
  )
);