Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7841217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:03:18+00:00 2026-06-02T16:03:18+00:00

For example i have algorithmic function, which calculates specific hash-code. Function itself is 300+

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:03:19+00:00Added an answer on June 2, 2026 at 4:03 pm

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

    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"
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment in which I need to create a function that tells
I have a function in java which is written with a recursive algorithm that
In my code, I have got function templates containing lambda expressions which depend on
Example: I have an class that inherits from UIImageView. An object creates an instance
Example: I have a selector like this, which I give to another method as
Example: I have a method -myFooBarMethod:withFoo:bar:moreFoo: and inside the implementation of that method I
Let's imagine I have an unknown function that I want to approximate via Genetic
I have a set S of small trees S[i] which I need to find
I have a treesort function which performs two distinct tasks, each with its own
I have a complex code base at work, and i created a small example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.