scala - Obtain the URI of a Controller method call -
scala - Obtain the URI of a Controller method call -
i'm writing play 2.3.2 application in scala.
in application i'm writing method phone call other controller method following:
def addtagtouser = corsaction.async { request => implicit val userrestformat = userformatters.restformatter implicit val inputformat = inputformatters.restformatter implicit val outputwriter = outputformatters.restwriter //update tag of user def updatetagtouserdb(value: jsvalue): future[boolean] = { val holder : wsrequestholder = ws.url("http://localhost:9000/recommendation/ advise") val complexholder = holder.withheaders("content-type" -> "application/json") complexholder.post(value).map(response => response.status match {//handle response case 200 => true case _ => false } ) } val jsondata = request.body.asjson //get json info jsondata match { case some(x) => x.validate[input] match { case jssuccess(input, _) => updatetagtouserdb(x).flatmap(status => status match { case true => future{ok} case _ => future{internalservererror("error on update users tags")} }) case e: jserror => future{badrequest("json bad formed")} } case none => future{badrequest("need json value")} }
}
but in code i've problem url create static, possible absolute uri of controller method in play??
how can create that??
as mentioned in reverse routing section of play docs, can accomplish next method call:
routes.application.advise()
note routes
exists in controllers
if in controllers
bundle can access reverse routes routes.controllername.methodname
.
from other parts of code need utilize qualified package, i.e. controllers.reverse.application.advise()
.
if controller method takes parameter need pass desired argument , actual route, illustration routes.application.somemethod(10)
.
reverse routing powerful asset in play toolbox frees repeating yourself. it's future proof in sense if alter route, alter reflected automatically whole application.
alternativethis approach may not best approach.
redirecting controller makes sense, sending request controller resides within same web app overkill , unnecessary. more wise if web app serves responses outside not request response itself.
you can avoid putting mutual logic somewhere else , utilize both controllers. according best practices controller lean one! improve layering life much easier.
scala playframework routing uri
Comments
Post a Comment