使用重定向我想要建立一個簡單的動作遊戲控制器用來檢查會話活動:播放/斯卡拉:在ActionBuilder
import play.api.mvc._
import scala.concurrent._
object AuthAction extends ActionBuilder[Request] {
def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[SimpleResult]) = {
if (request.session.isEmpty) {
//Need to redirect to login page
Redirect("/login")
} else {
//Session is found, continue Action as normal
block(request)
}
}
}
問題是,它不承認Redirect
。我如何才能在這個範圍內工作?我想在需要授權的控制器中使用此操作:
object Application extends Controller {
def index = AuthAction {
Ok(views.html.index("You are logged in."))
}
}
這兩個是不同的文件。
側面問題:invokeBlock[A]
和Request[A]
中的「A」究竟是什麼?
我在玩2.2.1,2.9.3斯卡拉,爪哇1.8 64位
UPDATE:試過,它沒有任何更多的給予任何錯誤,但它不工作 - 重定向似乎被忽略。
import play.api.mvc._
import scala.concurrent._
import play.api.mvc.Results._
object AuthAction extends ActionBuilder[Request] {
def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[SimpleResult]) = {
if (request.session.isEmpty) {
Redirect("/login")
}
block(request)
}
}
看來我有一點點接近,但它仍然看起來像一個死衚衕 - 'Redirect'不工作,也要求返回'未來[辛pleResult]'不管那是... – Caballero
啊,是的,Play 2.2想要一個Future.successful ...我相信else子句是必需的。 – mantithetical