繁体   English   中英

变量通过请求绑定到整个应用程序

[英]Variable bound to whole application through requests

我想要一个绑定到应用程序范围的变量(在Java中将是应用程序范围)。

我认为服务应该是为了达到我的目标而使用的东西。

我创建了服务测试

<?php

namespace Acme\MyBundle\Service;

class test {

    public $count;

    public function __construct() {
        $this->count = 0;
    }

    public function addCount() {
        $this->count++;
    }

}

我在services.yml中声明的

acme.my.service.test:
      class: Acme\MyBundle\Service\test

我在控制器中称呼它

public function testAction() {
        $this->get('acme.my.service.test')->addCount();
        return $this->render('AcmeMyBundle:Test:test.html.twig');
    }

但是,当我在树枝中检索它时,该值是1,无论我刷新多少或绑定到testAction的url上进行多个会话。 =>这意味着每次都调用构造函数。

那是正确的方法吗? 我以为服务创建一次,然后重用,但是我可能会误会。

你能启发我吗?

谢谢copndz

我找到了我想要的APC缓存系统。 易于使用,并集成到该学说中。

如何在Symfony 2中进行缓存?

http://docs.doctrine-project.org/en/latest/reference/caching.html

暂无
暂无

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

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