通过XML进行Symfony依赖注入

时间:2015-07-12 09:43:16

标签: php symfony dependency-injection

我使用的是组件(版本2.7),而不是symfony框架。

我正在使用PHP注入,但没关系,但我决定使用XML。我不知道如何将XML存储服务加载到ContainerBuilder对象中,

我正在以这种方式使用该组件:

$container = new ContainerBuilder();
$container->setDefinition('Crypt', new Definition('MiladRahimi\PHPCrypt\Crypt'));

1 个答案:

答案 0 :(得分:0)

该组件为nicely documented。您可以在官方文档中找到所需的一切。

具体来说,请阅读Setting up the Container with Configuration FilesCompiling the container。请务必dump the container for better performance

以下是从文档中加载xml文件的示例:

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');