scalaplayframeworkroutescustom-routes

Scala - passing defined values for optional parameters within routes file


So I have a routes file that calls a controller with several optional inputs. as per below

GET      /1.0/*path/documents             
controllers.DocumentController.list(path, format: Option[String], ref: Option[String], build: Option[String], files: Option[String], token: Option[String])

I'm looking to customize a URI that would pass defined or provided values for these options, specifically I'm looking to pass :ref & :build from the URI into their respective options below without changing the underlying controller or model calls.

GET      /1.0/*path/documents/:ref.:build         
controllers.DocumentController.list(path, format: Option[String], Some(ref), Some(build), files: Option[String], token: Option[String])

Currently the above gives this error.

')' expected but '(' found.

I've been using scala for ~3 weeks and have little formal training in OO or MVC development so please go easy on me :)

SOLUTION: Method must be defined to take these parameters as part of a function method rather than as options. see answer below.


Solution

  • Optional arguments can only be provided with the query option after the URL. Take for example the below route:

    GET      /1.0/*path/documents        
    controllers.DocumentController.list(path, format: Option[String], Option[ref], Option[build], files: Option[String], token: Option[String])
    

    These parameters can be provided by using a query appended to the URI after ?.

    For example:

    http://localhost:9000/1.0/pathvariable/documents?format=json&ref=master
    

    If the desired customized route is:

    GET      /1.0/*path/documents/:ref.:build
    

    Then the ref and build variables should not be defined as options but as regular input parameters that must be provided.

    Below example where list 2 is a new method taking the 3 parameters as well as some optional parameters:

    GET      /1.0/*path/documents/:ref.:build
    controllers.DocumentController.list2(path, ref: String, build: String, format: Option[String], files: Option[String], token: Option[String])