简体   繁体   English

哪里可以在Symfony2中存储简单的第三方类?

[英]Where to store simple 3rd party class in Symfony2?

I'm new to Symfony2 and I have faced some simple problem but I'm not sure how to manage with it. 我是Symfony2的新手,我遇到了一些简单的问题,但我不确定如何应对它。 I need to use one simple 3rd party class and I'm not sure where and how to store it in project structure. 我需要使用一个简单的第三方类,我不知道在项目结构中将它存储在何处以及如何存储。 Should I store is a Service in my Bundle or maybe I should store it in vendors directory? 我应该存储我的Bundle中的服务还是我应该将它存储在供应商目录中? And if I'll store it in vendors isn't it a bad practice to store there the libs that isn't Symfony supported vendors? 如果我将它存储在供应商中,那么存储不是Symfony支持的供应商的库并不是一个坏习惯吗?

Usually you include those in your project with Composer . 通常,您在Composer中包含那些项目。 I suggest you to take a look at packagist to look if there is a Composer package for your class, otherwise you can't require it with composer. 我建议你看看packagist ,看看你的班级是否有一个Composer包,否则你不能用composer来要求它。

Composer puts your classes in the vendor directory, you should put all 'vendors' (3th party libraries) there. Composer将您的类放在vendor目录中,您应该将所有“供应商”(第3方库)放在那里。 Take a look on where to put them in that directory, so that the Composer autoloader can autoload it. 看一下将它们放在该目录中的位置,以便Composer自动加载器可以自动加载它。

After that, it is recommend to create a bundle for that specific class. 之后,建议为该特定类创建一个包。 It is a best practise to create a service there. 在那里创建服务是最佳实践。 For instance, if your class is Foo you create a Acme\\FooBundle which loads the Foo service: 例如,如果你的类是Foo你创建一个加载Foo服务的Acme\\FooBundle

// src/Acme/FooBundle/DependencyInjection/AcmeFooExtension.php
<?php

namespace Acme\FooBundle\DependencyInjection;

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

class AcmeFooExtension extends Extension
{
    /**
     * this method loads the Service Container services.
     */
    public function load(array $configs, ContainerBuilder $container)
    {
         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

         // load the src/Acme/FooBundle/Resources/config/services.xml file
         $loader->load('services.xml');
    }
<!-- src/Acme/FooBundle/Resources/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <!-- Loads the \Foo class as a acme_foo.foo service -->
        <service id="acme_foo.foo"
            class="\Foo"
        ></service>
    </services>

</container>

Symfony itself stores the 3rd Party libraries at the vendors folder. Symfony本身将第三方库存储在vendors文件夹中。 Is a good practice to put your 3rd party class there too 将第三方课程放在那里也是一种很好的做法

If you don't know how to do it, probably this question will help. 如果您不知道该怎么做,可能这个问题会有所帮助。

I belive that using service container will be a good practice. 我相信使用服务容器将是一个很好的做法。 Anyway, service container is made up for storing third party depindencies and saving loose coupling. 无论如何,服务容器用于存储第三方的依赖性并且保存松耦合。

Look the docs , there is written how and why service container should be used. 查看文档 ,写下如何以及为什么应该使用服务容器。

Good luck. 祝好运。

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

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