Symfony normal Class DependencyInjection

Viewed 24

My goal is to get the projectdir from the kernel in a class

I have a class which i manually initiate

$foo = new FooHelper();

In this class I want to access the ParameterBagInterface

class FooHelper 
{
  protected $parameterBag;

  public function __construct(ParameterBagInterface $parameterBag)
  {
      $this->parameterBag = $parameterBag;
  
  }
}
$this->parameterBag->get('kernel.project_dir');

Error

ArgumentCountError: Too few arguments to function FooHelper::__construct(), 
0 passed in […] on line 91 and exactly 1 expected

I know there is the possibility to give the calling class the Parameterbag, which would solve it.

$foo = new FooHelper($parameterBag);

Is there a way to avoid giving the $parameterBag within the constructor and get the projectdir directly? Then the class could be initiated without $parameterBag which makes the usage of this helper class much less difficult.

0 Answers
Related