Symfony2 全局函数 [英] Symfony2 global functions

查看:19
本文介绍了Symfony2 全局函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有算法函数,它计算特定的哈希码.函数本身是 300 多行代码.我需要在捆绑包中的许多不同控制器中多次使用该功能.我在哪里可以存储我的 calculate_hash() 以在我的包中使用它?我可以从其他捆绑包访问它吗?我还可以编写可以访问实体管理器的全局 calculate_hash() 吗?

For example i have algorithmic function, which calculates specific hash-code. Function itself is 300+ lines of code. I need to use that functions many times in many different controllers in my bundle. Where can i store my calculate_hash() to use it in my bundle ? Can i access it from other bundles ? Can i also write global calculate_hash() which have access to entity manager ?

此处没有找到我的答案.

推荐答案

在 Symfony2 世界中,这显然属于服务.服务实际上是绑定到依赖注入容器的普通类.您可以向它们注入所需的依赖项.例如,假设函数 calculate_hash 所在的类是 AlgorithmicHelper.该服务拥有全局"功能.你定义你的类是这样的:

In the Symfony2 world, this is clearly belonging to a service. Services are in fact normal classes that are tied to the dependency injection container. You can inject them the dependencies you need. For example, say your class where the function calculate_hash is located is AlgorithmicHelper. The service holds "global" functions. You define your class something like this:

namespace AcmeAcmeBundleHelper;

// Correct use statements here ...

class AlgorithmicHelper {

    private $entityManager;

    public function __construct(EntityManager $entityManager) {
        $this->entityManager = $entityManager;
    }

    public function calculate_hash() {
        // Do what you need, $this->entityManager holds a reference to your entity manager
    }
}

然后需要让这个类知道 symfony 依赖容器.为此,您可以在 app/config/config.yml 文件中通过添加这样的 service 部分来定义您的服务:

This class then needs to be made aware to symfony dependecy container. For this, you define you service in the app/config/config.yml files by adding a service section like this:

services:
  acme.helper.algorithmic:
    class: AcmeAcmeBundleHelperAlgorithmicHelper
    arguments:
      entityManager: "@doctrine.orm.entity_manager"

就在服务下方,是服务 ID.例如,它用于在控制器中检索您的服务.之后,您指定服务的类,然后指定要传递给类的构造函数的参数.@ 符号表示传递对服务的引用,其 ID 为 doctrine.orm.entity_manager.

Just below the service, is the service id. It is used to retrieve your service in the controllers for example. After, you specify the class of the service and then, the arguments to pass to the constructor of the class. The @ notation means pass a reference to the service with id doctrine.orm.entity_manager.

然后,在您的控制器中,您执行以下操作来检索服务并使用它:

Then, in your controller, you do something like this to retrieve the service and used it:

$helper = $this->get('acme.helper.algorithmic');
$helper-> calculate_hash();

请注意,调用 $this->get('acme.helper.algorithmic') 的结果将始终返回相同的助手实例.这意味着,默认情况下,服务是唯一的.这就像有一个单例类.

Note that the result of the call to $this->get('acme.helper.algorithmic') will always return the same instance of the helper. This means that, by default, service are unique. It is like having a singleton class.

有关更多详细信息,我邀请您阅读 Symfony2 书籍.也检查这些链接

For further details, I invite you to read the Symfony2 book. Check those links also

  1. Symfony2 书中的服务容器部分.
  2. 我在访问控制器外的服务时给出的答案,此处.

希望有帮助.

问候,
马特

这篇关于Symfony2 全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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