0

I have a class that doesn't have access to the service container (because it does not extends the Controler class).

How can I get access to the parameters from parameters.ini in that class?

1 Answer 1

3

There are two ways I can think of:

  1. Define your class as a service and inject the parameters into it. This is the cleanest solution.

    Ex:

    // parameters.ini
    my_param: fooBar
    
    // services.yml
    tests.example:
      class: some\class
      arguments: [%my_param%]
    

    fooBar will now be passed to your class constructor.

  2. Define your class as a service and inject the container service into it. This is not recommended and I'm only listing it because it's technically possible.

Sign up to request clarification or add additional context in comments.

1 Comment

After defining your class as service you must call the service exclusively from the service container ($this->get('tests.example'); in the controller) and must not create it manually. Take a look at the official documentation at symfony.com/doc/current/book/service_container.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.