0

I have a interesting situation I have not yet encountered with Symfony...

I have a bundle, which provides dynamic configuration to the entire application. I've registered the service of the bundle and it all works.

MyNamespace\MyBundle\Site\Vars:
    arguments:
      $path: '%env(APP_PARAM)%'

However, there are templates which rely on the vars, which the service registers with Twig globals. I am getting an "Variable "XTZ" does not exist." unless I explicitly request the service "Vars" as per each controller::action:

public function someAction(): Response -> FAILS

public function someAction(Vars $vars): Response -> WORKS!?!

I don't want to have every controller::action, commands, etc requires this explicit-ness.

I thought maybe, Symfony supports like an eager-loaded service option or something.

Any thoughts?

EDIT | Method in the class / services Vars:

public function loadIntoTwigGlobals(): void
{
    $items = Yaml::parseFile($this->path);

    $this->setArray($items);
    $this->twig->addGlobal('site', $items['site']);
}

The Twig\Environment is injected at construction.

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
  • You can add a property and a constructor to your controller class and use dependency injection at the class level. You can also do that in a BaseController that your other controllers can extend from. You can also make your service public and use `$this->container->get('service_id')` to fetch it. – Julien B. Jun 02 '22 at 02:11
  • Maybe update your question and show how your Vars service is being injected into your `twig globals`? And perhaps an example of a twig template accessing it. No particular reason why it should not work. – Cerad Jun 02 '22 at 14:34
  • I added the function call that loads the twig variables. I've tried calling this as part of the service configuration too, no dice!! I feel as tho the containers are proxied to support lazy loading? And it's only once I've explicitly stated I need a service as per the type-hinted argument in any controller/service, that seems to trigger Symfony to call the actual load YAML – Alex.Barylski Jun 02 '22 at 14:57
  • @Cerad also to be clear, I'm not injecting a Vars service into twig per se. Vars is a service, which when instantiated, should open a yaml file and populate the Twig globals. But the globals aren't available until the Vars service is explicitly used in a controller or other service. My work around was to add a kernel listener, type-hint the Vars service and call the loadIntoTwigGlobals() explicitly. This makes the twig globals available on every request. Kind of smells, but it works. I'd love a better solution tho? – Alex.Barylski Jun 02 '22 at 15:06
  • A kernel listener would do the trick but I would I would seriously question the need to parse a yaml file on every request. I think that is the origin of the smell you are encountering. Seems like the sort of thing a compiler pass would do. But maybe you need it. – Cerad Jun 02 '22 at 15:13
  • Normally I would agree, but this is for a CMS. Vars are site wide variables that the user can change, thus the storage in YAML/DB, they are not configuration values, they can change after compilation of containers. I will try the compiler pass next, thanks! – Alex.Barylski Jun 02 '22 at 15:25
  • 1
    Compiler passes are strictly for the compilation of containers. Maybe some sort of caching? Hard to say. – Cerad Jun 02 '22 at 15:49

0 Answers0