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

  • SEARCH
  • Home
  • 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 5963227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:13:07+00:00 2026-05-22T19:13:07+00:00

I’m building a site that relies quite heavily on a third party API so

  • 0

I’m building a site that relies quite heavily on a third party API so I thought it would make sense to package up the API wrapper as a service, however I’m starting to find instances where it would be useful to have access to it outside of a controller such as in an entity repository.
Also related to that is it would be useful to be able to get access to config values outside of a controller (again such as in an entity repository).

Can anyone tell me if this is possible and if not is there a suggested approach to doing this kind of thing?

thanks for any help

  • 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-05-22T19:13:08+00:00Added an answer on May 22, 2026 at 7:13 pm

    The Symfony distribution relies heavily on dependency injection. This means that usually, dependencies are injected directly into your object via the constructor, the setters or via other means (like reflection over properties). Your API wrapper service is then a dependency for other objects of your application.

    That being said, it would be rather difficult to inject this service in an entity repository’s constructor because it already requires some other parameters and I think it would not be possible to inject them because of the way we request the repository for an entity.

    What you could do is to create another service which will be responsible of doing the work you were about to do in the entity repository. This way, you will be able to inject the entity manager, which will be used to retrieve the entity repository, you custom service and also another service holding your configuration values (There are other ways to share configuration values).

    In my use case, I use a Facebook helper service that wraps Facebook API calls. This service is then injected where I need it. My entity repository is only responsible of doing database calls so it receives only the arguments it needs and not the whole dependency. Thus, it will not receive the helper but rather only the arguments needed to do a request, for example, a Facebook user id. In my opinion, this is the way to do it since I think the entity repository should not have dependencies on such helper objects.

    Here a small example using YAML as the configuration:

    # app/config/config.yml
    services:
      yourapp.configuration_container:
        class: Application/AcmeBundle/Common/ConfigurationContainer
        # You could inject configurations here      
    
      yourapp.api_wrapper:
        class: Application/AcmeBundle/Service/ApiWrapperService
        # Inject other arguments if needed and update constructor in consequence    
    
      yourapp.data_access:
        class: Application/AcmeBundle/Data/Access/DatabaseAccessService
        arguments: 
          entityManager: "@doctrine.orm.entity_manager"
          apiWrapperService: "@yourapp.api_wrapper"
          configuration: "@yourapp.configuration_container"
    
    # Application/AcmeBundle/Common/ConfigurationContainer.php
    public ConfigurationContainer
    {
       public function __construct()
       {
           // Initialize your configuration values or inject them in the constructor
       }
    }        
    
    # Application/AcmeBundle/Service/ApiWrapperService.php
    public ApiWrapperService
    {
       public function __construct()
       {
           // Do some stuff
       }
    }
    
    # Application/AcmeBundle/Data/Access/DatabaseAccessService.php
    public DatabaseAccessService
    {
        public function __construct(EntityManager $entityManager, ApiWrapperService $apiWrapperService, ConfigurationContainer $configuration)
        {
            ...
        }
    }
    

    The at sign (@) in the config.yml file means that Symfony should inject another service ,having the id defined after the at sign, and not a simple string. For the configuration values, as I said previously, there is other means to achieve the same goal like using parameters or a bundle extension. With a bundle extension, you could define the configuration values directly into the config.yml and your bundle would read them.

    In conclusion, this should give you the general idea of injecting services. Here a small list of documentation on the subject. Alot of links use the XML service definition instead of the YAML definition but you should be able to understand them quite easily.

    1. Symfony Official DI
    2. Fabien Potencier’s articles on DI
    3. Richard Miller’s articles on DI (Check in his blog for the other DI articles)

    Take note that the configuration I’m giving is working for Beta1 of Symfony2. I didn’t update yet to Beta2 so there could be some stuff not working as they are in the Beta2 version.

    I hope this will help you defining a final solution to your problem. Don’t hesitate to ask other questions if you want clarifications or anything else.

    Regards,
    Matt

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build

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.