3

I am new to play framework (scala), and I still getting my way through my first web application. I just created a first method in my controller, index:

 def index = UserAwareContextAction { implicit request =>
val subset = request.params.get("subset").getOrElse("all")
request.user match {
  case Some(user) => selectIndex(request, subset)
  case _ => Ok(views.html.index())
}

Now I need to figure out how to actually add params to my index request, I have a navigation scala class:

  val index                     =   GET.on(root).to{Application.index_}

So I am not pretty sure how this should be related , where to declare request params , how to pass it ? I don't know why play documentation doesn't seem relevant to me. Please any help, or a useful tutorial on how to get stared, I will appreciate a lot.

mahoosh
  • 553
  • 1
  • 7
  • 21
  • The second snippet seems to be a redirect. The first one has request as an implicit parameter (it's provided by the framework for every action). Can you be a bit more specific about what you want to achieve? – Ashalynd Oct 25 '13 at 17:25

2 Answers2

2

Typically, play controllers with parameters look like this:

// Controller
def get(id: Long) = Action { implicit request =>
  // do stuff
  val someArgument = ...
  Ok(views.html.index(someArgument))

// route
GET    /account/:id      AccountController.get(id: Long)

If you are trying to access query string params, those can be accessed from the implicit request by simply calling request.queryString

mantithetical
  • 1,755
  • 2
  • 16
  • 21
2

There are minimum two approach.

First:

you may let Play to parse params from url for you: for example you need to pass user_id to your index page, then your url for GET request may be like this:

/index/1

and in plays root file :

GET /index/:user_id      Controllers.Application.index(user_id : Int)

so in this case play will parse user_id as 1 for you from your request url. Or your request may be as follows :

/index?user_id=1 and in your root :

GET /index      Controllers.Application.index(user_id : Int) 

and again play parse it for you, user_id as 1.

In two cases you get this user_id as a parameter in your controller :

def index(user_id : Int) = Action{implicit request =>
       // user_id == 1
       ......
       Ok("")
}

Another:

get params directly from request in your controller for example as a Map using Request method queryString and your controller may look like this:

 def index = Action{ request =>
 // you get your params as Map[String,Seq[String]] where `key` is you parameter `name` and value is //wraped in to a Seq 
    val params = request.queryString
     // or if you want to flatten it to Map[String,Option[String]] for example    
       val params = request.queryString.map {case(k,v) => k->v.headOption}  
       .....    
       Ok("")
        }

for this case root is simply : GET /index Controllers.Application.index

arussinov
  • 1,237
  • 11
  • 16
  • Well I am afraid both methods are correct when you are using the default conf/routes .. in my case I am using the nav.scala class which is a library for navigation , but I can tell how to pass params ! – mahoosh Oct 28 '13 at 16:53