简体   繁体   English

Symfony2-将供应商注册为服务

[英]Symfony2 - register vendor as service

I want to register vendor library as service, like doctrine, which anybody can access via $container->get('doctrine') . 我想将供应商库注册为服务,例如主义,任何人都可以通过$container->get('doctrine') I want to register with that way another vendors, for example PhpExcel ( $container->get('phpexcel') ). 我想用这种方式注册其他供应商,例如PhpExcel( $container->get('phpexcel') )。

See services.yml file from liuggio/ExcelBundle 请参阅liuggio / ExcelBundle的 services.yml文件

parameters:
    xls.phpexcel.class: PHPExcel

services:
    xls.phpexcel:
        class: %xls.phpexcel.class%

And then $container->get('xls.phpexcel'); 然后$container->get('xls.phpexcel');

As long as 3rd party classes respect DI by not instantiating their dependencies themselves, you can register them as services just like you would your own classes. 只要第三方类通过不实例化DI的依赖关系来尊重DI,就可以像注册自己的类一样将它们注册为服务。 Read the Service Container chapter to get more information on how to do that. 阅读服务容器一章以获取有关如何执行此操作的更多信息。

You should use a working bundle for that. 您应该为此使用一个工作包。 Find all bundles available for composer at packagist.org . 在packagist.org上找到适用于作曲家的所有捆绑包。 The liuggio/ExcelBundle should do the job you want. liuggio / ExcelBundle应该可以完成您想要的工作。 After installation you can use PHPExcel as a service: 安装后,您可以将PHPExcel用作服务:

$excelService = $this->get('xls.service_xls5');

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

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