Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect application mode in Play 2.x

From within a Play 2.1 application, how would I programmatically determine which mode the application is running in (i.e., Development vs. Production)?

For example, it would be useful to be able to do something like this from inside a template:

<p>@if(__some_play_API_call__ == Dev) { <b>Development mode</b> }</p>

In the Play 2.0 API documentation, there appears to be a mode property of the play.api.Application class... however, I am unsure about how to get at the instance of the currently running application.

like image 822
kes Avatar asked Jan 30 '13 20:01

kes


2 Answers

You can access the current Appliction via

play.api.Play.current() 

to find out the mode try

play.api.Play.current().mode() 

or simply use

play.api.Play.isDev(play.api.Play.current()) 
like image 92
maxmc Avatar answered Oct 04 '22 04:10

maxmc


In Play 2.5.x the play.Play.isDev() method is deprecated, one has to use dependency injection:

import javax.inject.Inject;  public class Example {      @Inject     private play.Environment environment;      public void myMethod() {         if (environment.isDev()) {           ...         }     } } 

Or equivalently in Scala:

class ErrorHandler @Inject()(environment: Environment) {    def myMethod() = {     if (environment.isDev) {       ...     }   }  } 

environment.isDev() returns a Boolean, which one can easily pass to a template. No need to use implicit variables as described here.

like image 43
koppor Avatar answered Oct 04 '22 03:10

koppor