Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

php - Symfony2 global functions

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 ?

Didn't find my answer here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
    }
}

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"

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();

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.

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

  1. The service container section from Symfony2 book.
  2. An answer I gave on accesing service outside controllers, here.

Hope it helps.

Regards,
Matt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...