Symfony2全局函数 [英] Symfony2 global functions

查看:131
本文介绍了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 Acme\AcmeBundle\Helper;

// 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依赖容器.为此,您可以通过添加service部分来定义您在app/config/config.yml文件中的服务:

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: Acme\AcmeBundle\Helper\AlgorithmicHelper
    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. 我在此处上对访问控制器外部服务的回答.
  1. The service container section from Symfony2 book.
  2. An answer I gave on accesing service outside controllers, here.

希望有帮助.

此致,
马特

Regards,
Matt

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

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