在旧版应用程序中,我们引入了在Definition
Extension
的容器
$serviceId = ($alias == 'default') ? 'm6_statsd' : 'm6_statsd.'.$alias;
$definition = new Definition('M6Web\Component\Statsd\Client');
$definition->setScope(ContainerInterface::SCOPE_CONTAINER);
$definition->addArgument($usedServers);
$container->setDefinition($serviceId, $definition);
//编译容器的方法
private function loadContainer()
{
// Ok that's just plain bad, but at least it works. I need this as a static because controllers can't access application
// and I can't put container in models because it would be inherited by front office.
self::$container = new ContainerBuilder();
$helpscoutExtension = new HelpscoutExtension();
self::$container->registerExtension($helpscoutExtension);
self::$container->loadFromExtension($helpscoutExtension->getAlias());
$statsdExtension = new StatsdExtension();
self::$container->registerExtension($statsdExtension);
self::$container->loadFromExtension($statsdExtension->getAlias());
$loader = new YamlFileLoader(self::$container, new FileLocator(C_PATH_GLOBAL_CONF));
$loader->load('bov6.yml');
self::$container->compile();
dump(self::$container);
}
容器已编译,但是当我转储容器时,它只显示“定义”并且没有可用的服务
我错过了什么?为什么定义没有转化为服务?
答案 0 :(得分:3)
如果您没有为服务调用$container->get('service')
方法,则表示该服务尚未创建。
首次尝试访问时,会实例化服务。