Documentation

You are viewing the documentation for the 2.2.1 release in the 2.2.x series of releases. The latest stable release series is 3.0.x.

§Application global settings

§The Global object

Defining a Global object in your project allows you to handle global settings for your application. This object must be defined in the default (empty) package and must extend GlobalSettings.

import play.api._

object Global extends GlobalSettings {

}

Tip: You can also specify a custom GlobalSettings implementation class name using the application.global configuration key.

§Hooking into application start and stop events

You can override the onStart and onStop methods to be notified of the events in the application life-cycle:

import play.api._

object Global extends GlobalSettings {

  override def onStart(app: Application) {
    Logger.info("Application has started")
  }

  override def onStop(app: Application) {
    Logger.info("Application shutdown...")
  }

}

§Providing an application error page

When an exception occurs in your application, the onError operation will be called. The default is to use the internal framework error page:

import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent.Future

object Global extends GlobalSettings {

  override def onError(request: RequestHeader, ex: Throwable) = {
    Future.successful(InternalServerError(
      views.html.errorPage(ex)
    ))
  }

}

§Handling missing actions and binding errors

If the framework doesn’t find an Action for a request, the onHandlerNotFound operation will be called:

import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent.Future

object Global extends GlobalSettings {

  override def onHandlerNotFound(request: RequestHeader) = {
    Future.successful(NotFound(
      views.html.notFoundPage(request.path)
    ))
  }

}

The onBadRequest operation will be called if a route was found, but it was not possible to bind the request parameters:

import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent.Future

object Global extends GlobalSettings {

  override def onBadRequest(request: RequestHeader, error: String) = {
    Future.successful(BadRequest("Bad Request: " + error))
  }

}

Next: Intercepting requests