I'm deploying my first Phoenix Application, and I've specified the values of a variable in my Environment Files (dev.exs
and prod.exs
).
Now I'm trying to figure out how to access them in my Controllers.
# config/dev.exs
config :my_app, MyApp.Endpoint,
http: [port: 4000],
debug_errors: true,
cache_static_lookup: false,
my_var: "DEVELOPMENT VALUE"
# config/prod.exs
config :my_app, MyApp.Endpoint,
http: [port: {:system, "PORT"}],
url: [host: "example.com"],
my_var: "PRODUCTION VALUE"
Okay, found something. Elixir's
Application.get_env/3
is the way to go:But the problem with this is that the accessor command becomes very long for the current situation:
A better way would be to define them in a separate section:
and access them like this:
Or you could fetch a
list
of all key-value pairs: