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 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::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 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
}
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(
'Zend\Loader\StandardAutoloader' => 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
).