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 {
}
§Hooking into application start and stop eventsTip: You can also specify a custom
GlobalSettings
implementation class name using theapplication.global
configuration key.
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
Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request. Have questions or advice to share? Go to our community forums to start a conversation with the community.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4