Skip to content

Instantly share code, notes, and snippets.

@timothyklim
Created June 12, 2012 17:22
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timothyklim/2918814 to your computer and use it in GitHub Desktop.
Save timothyklim/2918814 to your computer and use it in GitHub Desktop.
After I talked with co-author of play with topic subdomains, I was a little bit confused. My current framework doesn't support my thoughts and I was started write some code!
// app/controllers/Application.scala
trait SubdomainController extends Controller {
def WithSubdomain(f: => String => Request[AnyContent] => Result) = Action { implicit request =>
val splitDomain = request.domain.split("\\.")
if (splitDomain.length < 2) {
BadRequest("Domain not found!")
} else {
f(splitDomain.head)(request)
}
}
}
<!-- app/views/some/index.scala.html -->
@(implicit request: play.api.mvc.Request[Any], subdomain: String)
Hi! You're on <a href="@helper.linkWithSubdomain(routes.SomeController.index)">@subdomain</a> subdomain.
// app/controllers/SomeController.scala
object SomeController extends SubdomainController{
def index = WithSubdomain { implicit domain => implicit request =>
Domain.findByName(domain) match {
case Some(foundDomain: Domain) =>
Ok(views.html.some.index())
case _ => Ok("Nothing found!")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment