Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Play External Redirect

As I'm working on implementing TinyUrl, I want to redirect users to a webpage based on the input hash.

  def getTask(hash: Int) = Action {
    val url: Option[String] = Task.getTask(hash)
    // redirect to show(url) 
  }

However, I don't know how to redirect the user to an external URL.

I saw this related post, but I encountered this compile-time error when I used redirect

not found: value redirect

like image 870
Kevin Meredith Avatar asked Dec 04 '25 22:12

Kevin Meredith


1 Answers

redirect doesn't exist.
But Redirect, which is a member of the play.api.mvc package, does.

Here's an example of what your action should look like:

import play.api.mvc._

def getTask(hash: Int) = Action {
  val url: Option[String] = Task.getTask(hash)

  url match {
    case Some(url) => Redirect(url)
    case None => NotFound("This URL leads nowhere. :(")
  }
}
like image 136
Samy Dindane Avatar answered Dec 06 '25 14:12

Samy Dindane