在 Symfony 2 中,我在哪里放置通用库代码? [英] Where do I put generic library code in Symfony 2?

查看:28
本文介绍了在 Symfony 2 中,我在哪里放置通用库代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题:我应该在哪里放置与 Controller Utilities Service Class 具有相似特征的代码,如 Benjamin Eberlei 在这篇博文(http://www.whitewashing.de/2013/06/27/extending_symfony2__controller_utilities.html)?

暂时,我把它放在了:src/ProjectName/Library

背景

我注意到以下几点:

  • 这个逻辑不属于特定的bundle,事实上它适用于一个人要创建的所有bundle.
  • 此逻辑通常属于特定于应用程序的库,而不是作为捆绑包的一部分,因为捆绑包中的控制器将扩展或利用此库代码.

我找到了一些主题相似但不完全是我想要的问题的答案

仅基于我对 SO 的研究,这个问题似乎在某种程度上被踩死了,但我认为之前提出的问题围绕着我真正想要的.无论如何,我似乎有以下选择:

  • 将这些类型的扩展放在一个包中 - 不适用,因为我正在开发的功能类型本质上是扩展框架代码.
  • 为所有库代码所在的项目创建供应商目录 - 如果这确实是最佳实践,那么这实质上意味着我必须通过私有使库可用在 composer 中使用 repo,但这意味着我必须维护一个单独的代码库.
  • 创建某种位于 src/Company/SomeNamespace 中的伪连接器包 - 我什至不知道这是否完全可行,但如果它符合SF 最佳做法,我会进一步研究.

再次为简洁起见:在 Symfony 2 中,我应该将提供通用、全局功能的类放在哪里?

提前致谢.

解决方案

本文档关于 composer 存储库 是一个很好的参考,关于不支持 Composer 的包的文档 应该是你要找的.

另外我想注意关于VCS的文档当你需要 fork 一个 Bundle 并覆盖原来的 Bundle 时使用(我用了很多次).

您可以执行以下操作 - 它不需要您拥有包装系统或其他任何东西.您只需要将包作为 zip 文件放入您的计算机可使用 URL 访问的文件中.

<代码>{存储库":[{"类型": "包",包裹": {"name": "我的/包裹","版本": "1.0.0",距离":{"url": "https://github.com/my/package/archive/master.zip",类型":拉链"},自动加载":{psr-0":{"My\\Package\\": "src/"}}}}],要求": {我的/包":1.0.0"}}

如果您的包不支持 PSR-0,那么您需要使用 选项"classmap" 否则你的包支持 PSR-0 你需要使用 该选项psr-0.

Quick question: Where should I be putting code that has similar characteristics to the Controller Utilities Service Class as described here on this blog post by Benjamin Eberlei (http://www.whitewashing.de/2013/06/27/extending_symfony2__controller_utilities.html)?

For the interim, I've placed it inside: src/ProjectName/Library

Context

I've noted the following:

  • This logic does not belong to a specific bundle, in fact it applies to all bundles one would be creating.
  • This logic usually belongs in an application specific library, as opposed to being part of a bundle as controllers within bundles would either extend or utilise this library code.

I found some answers to questions that are similar in theme, but not exactly what I was after

Based on my research here on SO alone, this question seems to have been trodden to death somewhat, but I think the questions asked previously skirt around what I'm actually after. Regardless, it would seem that I have the following options:

  • Putting these types of extensions in a bundle - not applicable as the type of functionality I'm developing is essentially extending the framework code.
  • Creating a vendor directory for the project where all the library code would go - if this is indeed the best practice, then it would essentially mean that I'd have to make the library available via private repo in composer, but that means that I'd have to maintain a separate codebase.
  • Create some sort of pseudo connector bundle that lives in the src/Company/SomeNamespace - I don't even know if this is at all the way to go, but if it is in keeping with SF best practice, I'll look into it further.

The question, again, for brevity: Where do I put classes which provide generic, global functionlity in Symfony 2?

My thanks in advance.

解决方案

This documentation about composer repositories is a great reference and the documentation about packages that don't support Composer should be what you are looking for.

Also I'd like to note the documentation about VCS which is often used when you need to fork a Bundle and override the original one (I used it quite a few times).

You can do the following - it won't require you to have a packaging system or whatever. You just need to put the package as a zip accessible by your computer using a URL.

{
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "my/package",
                "version": "1.0.0",
                "dist": {
                    "url": "https://github.com/my/package/archive/master.zip",
                    "type": "zip"
                },
                "autoload": {
                    "psr-0": {
                        "My\\Package\\": "src/"
                    }
                }
            }
        }
    ],
    "require": {
        "my/package": "1.0.0"
    }
}

If your package doesn't support PSR-0 then you need to use the option "classmap" otherwise your package supports PSR-0 you need to use the option psr-0.

这篇关于在 Symfony 2 中,我在哪里放置通用库代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆