Skip to content
July 14, 2012 / edeustace

Adding routes programmatically to Play 2.* apps

There’s some discussion about how one can programmatically add routes to a Play 2.* app. It appears that you can’t add them to the generated Routes class, but you can handle the web requests yourself by overriding ‘onRouteRequest’.

</code>
<code>override def onRouteRequest(request: RequestHeader): Option[Handler] = {
  //do our own path matching first - otherwise pass it onto play.
  request.path match {
    case "/injectedRoute" => Some(controllers.Application.customRoute)
    case _ => Play.maybeApplication.flatMap(_.routes.flatMap {
      router =>
      router.handlerFor(request)
    })
  }
}</code>
<code>

Here’s an example on github: https://github.com/edeustace/play-injected-routes-example

See this question too: http://stackoverflow.com/questions/10116286/programatically-adding-a-route-in-play2-0/11483556

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: