Created
April 9, 2012 16:20
-
-
Save julienrf/2344517 to your computer and use it in GitHub Desktop.
How to implement a custom PathBindable with Play 2
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
def show(article: Article) = Action { | |
Ok(views.html.article(article)) | |
} |
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
case class Article(id: Long, name: String, price: Double) |
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
implicit def articlePathBindable(implicit longBinder: PathBindable[Long]) = new PathBindable[Article] { | |
def bind(key: String, value: String): Either[String, Article] = | |
for { | |
id <- longBinder.bind(key, value).right | |
article <- Article.findById(id).toRight("Article not found").right | |
} yield article | |
def unbind(key: String, article: Article): String = | |
longBinder.unbind(key, article.id) | |
} |
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
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings( | |
routesImport += "binders._" | |
) |
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
override def onBadRequest(request: RequestHeader, error: String) = error match { | |
case "Article not found" => Redirect(controllers.routes.Articles.list) | |
case _ => super.onBadRequest(request, error) | |
} |
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
GET /show/:article controllers.Articles.show(article: models.Article) |
This is slick, thanks
In the Build.scala I was having a problem with the compile not finding routesImport
. In Play 2.4 it was moved, so now you need to add import play.sbt.routes.RoutesKeys.routesImport
to the Build file
Hi, could someone help with this question? https://stackoverflow.com/questions/66404308/play-framework-cannot-find-querystringbinders
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
QueryStringBindable[A]
works basically the same way, but its bind method returns anOption[Either[String, A]]
: if the parameter were not present in the query string it should returnNone
, otherwiseSome(Left(error))
in case of binding error orSome(Right(value))
in case of binding success.What is interesting with
QueryStringBindable
is that you can pull several parameters from the query string to bind your value. E.g.:To be used with the following routes file:
The following requests would match: