A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.playframework.com/documentation/2.5.x/api/scala/play/api/mvc/PathBindable.html below:

PathBindable - play.api.mvc.PathBindable

Binder for URL path parameters.

You can provide an implementation of PathBindable[A] for any type A you want to be able to bind directly from the request path.

For example, given this class definition:

case class User(id: Int, name: String, age: Int)

You can define a binder retrieving a User instance from its id, useable like the following:

// In your routes:
// GET  /show/:user      controllers.Application.show(user)
// For example: /show/42

object Application extends Controller {
  def show(user: User) = Action {
    ...
  }
}

The definition of binder can look like the following:

object User {
  implicit def pathBinder(implicit intBinder: PathBindable[Int]) = new PathBindable[User] {
    override def bind(key: String, value: String): Either[String, User] = {
      for {
        id <- intBinder.bind(key, value).right
        user <- User.findById(id).toRight("User not found").right
      } yield user
    }
    override def unbind(key: String, user: User): String = {
      intBinder.unbind(key, user.id)
    }
  }
}

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