8

I know... stay away from globals.

The thing is I really, really, need to have a value that is accessible and, most important modifiable from different parts of the application. It's a counter for some actions that I need to watch for debug purposes. Googling for anything related to Symfony and globals, always got me to results that suggests using the Container parameters or Twig globals, but the thing is that according to Symfony documentation:

You can only set a parameter before the container is compiled: not at run-time.

The Twig globals are pretty much out of scope given that I need them in controller, not in view.

IMHO, both of these solutions are more like constants than variables.

So, the question is: Is there a best practice to obtain what I need using Symfony or should I just use the PHP globals?

Thank you!

5
  • Store it in session? Make service and get this service from container? Commented Jun 25, 2018 at 14:10
  • Create a service with a private property $counter and a setter and a getter to set or get the value. A service class will be instantiated automatically and can be reached from everywhere where you can use the container. I think you could even write a destructor that stores the new value when Symfony terminates. symfony.com/doc/current/… Commented Jun 25, 2018 at 14:25
  • Thanks, using a service sound like the proper way. Please post an answer! Commented Jun 25, 2018 at 14:27
  • @MaxDamage Is this something you want? Dynamically creating a global twig parameter in controller and access it in twig template Commented Jun 25, 2018 at 17:57
  • @BentCoder not quite. I didn't needed in twig, I needed to be application-wide and modifiable. Commented Jun 26, 2018 at 7:45

2 Answers 2

9

Create a service (e.g: ApplicationGlobals) with a private property $counter and public setter and getter to read and write the value. A service class will be instantiated automatically and can be reached from everywhere where you can use the container (get it in the controller or pass it as an argument to other services).

Latter you will be able to add multiple variables, when needed, with their proper getters and setters and use them in your project.

I think you could even write a destructor that stores the new value when Symfony terminates. http://symfony.com/doc/current/service_container.html#creating-configuring-services-in-the-container

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

Comments

0

Why not use the parameters section in services.yaml ?

It seems that you can also define/modify variables programmatically :

$container->parameters()->set('app.admin_email', '[email protected]');

https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration https://symfony.com/doc/current/configuration.html#configuration-parameters

1 Comment

I guess based on the second note here: symfony.com/doc/4.2/service_container/… saying You can only set a parameter before the container is compiled: not at run-time., so while you can set the value, you might not be able to update it during run-time, which is more like a constant than a variable.

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.