Reputation: 5428
In Play! 2.0, one can use Global
as documented here. Global
needs to be in the default (empty) package.
I also need globals in my application, and some of them need to be available to the methods in Global
. Thus, I put them in Global.scala like so:
package object globals {
lazy val foo = Play.maybeApplication.flatMap(_.configuration.getString("foo")).getOrElse("default_value_of_foo")
}
And then I use it in my controllers like this:
globals.foo
Is there a better way?
Upvotes: 3
Views: 771
Reputation: 5103
I think this question is more about general software design than it is about Play Framework. If you truly need a bunch of random properties why not create your own object?
object Configuration {
val timeout = Play.maybeApplication.flatMap(
_.configuration.getString("timeout")
).getOrElse(0))
}
But usually the values belong to some other logical entity that is being configured.
Upvotes: 1