nowiko
nowiko

Reputation: 2567

get container parameter in my custom classes Symfony2

I have the class, it declare as service. When I get() my service I run some method and this method require two params what I want to let user configure in config.yml. How I can get these parameters in this class? Maybe exist some way to do this in my service definition? Or I need extend my class from ContainerAware (if I am right its bad practice)? Thanks!

Upvotes: 2

Views: 4326

Answers (2)

felipep
felipep

Reputation: 2512

You can use call them using the constructor

acme.your_service:
    class: Acme\DemoBundle\YourService
    arguments: [%param1%]

in the class

class YourService {
   protected $param1;

   public function __construct($param1) {
      $this->param1 = $param1;
   }
}

Upvotes: 2

Tomasz Madeyski
Tomasz Madeyski

Reputation: 10910

You can inject parameters into your service using %param_name% syntax

services.yml

services:
    your_service:
        class: Acme\DemoBundle\YourClass
        arguments: [@some.other.service, %my_parameter%]

parameters.yml

parameters:
    my_parameter: my_value

Upvotes: 3

Related Questions