简体   繁体   English

通过XML的Symfony依赖注入

[英]Symfony Dependency Injection via XML

I'm using the component (version 2.7) not the symfony framework. 我使用的是组件(2.7版),而不是symfony框架。

I'm using PHP to inject and it is OK but I decided to use XML. 我使用PHP进行注入,可以,但是我决定使用XML。 I don't know how to load XML stored services into ContainerBuilder object, 我不知道如何将XML存储的服务加载到ContainerBuilder对象中,

I'm using the component this way: 我以这种方式使用组件:

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

The component is nicely documented . 该组件已很好地记录在案 You'll find everything you need in the official docs. 您会在官方文档中找到所需的一切。

Specifically, read on Setting up the Container with Configuration Files and Compiling the container . 具体来说,请阅读“ 使用配置文件设置容器编译容器” Make sure you dump the container for better performance . 确保丢弃容器以获得更好的性能

Here's an example of loading an xml file, taken from the documentation: 这是一个加载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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM