Created
June 12, 2012 17:22
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/helpers/Link.scala | |
import play.api._ | |
import play.api.mvc._ | |
package views.html.helper { | |
object linkWithSubdomain { | |
def apply(path: play.api.mvc.Call, ssl: Boolean = false)(implicit subdomain: String, request: RequestHeader): String = { | |
linkToSubdomain(subdomain = subdomain, path = path, ssl = ssl) | |
} | |
} | |
object linkToSubdomain { | |
def apply(path: play.api.mvc.Call, subdomain: String, ssl: Boolean = false)(implicit request: RequestHeader): String = { | |
val schema = if (ssl) "https" else "http" | |
if (request.host.split("\\.").head == subdomain) | |
"%s://%s%s" format(schema, request.host, path.toString) | |
else | |
"%s://%s.%s%s" format(schema, subdomain, request.host, path.toString) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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