Dawid Ohia
Dawid Ohia

Reputation: 16445

How to get an access to the parameters in non-controller class in Symfony2?

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?

Upvotes: 0

Views: 1692

Answers (1)

Steven Mercatante
Steven Mercatante

Reputation: 25305

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.

Upvotes: 3

Related Questions