Symfony 2-我应该把实用程序类放在哪里


Symfony 2 - Where should I put a utility class?

我正在创建一个类,该类将有一个公共方法,该方法返回由参数索引的值。我现在只有一捆。我目前拥有的捆绑包中的目录是:

/Controller
/DataFixtures
/DependencyInjection
/Document
/Entity
/Resources
/Tests

像这样布置一个班级的惯例是什么?

Symfony官网建议src/AppBundle/Utils

来源:http://symfony.com/doc/current/best_practices/business-logic.html

您的问题有点主观,但根据Bundle Structure and Best Practices中概述的内容,Bundle只是按名称命名的代码。如果实用程序类是一级的,为什么不把它放在Bundle的根目录中呢?

Namespace Bundle'HelloBundle;
Class Utility {
    public static function returnIndexedValueByParameter($parameter) {
        ...
    }
}

文件名:

Bundle/HelloBundle/Utility.php