1
votes

I'm looking to do some prototype/exploratory type work with Scala/Play, and I'd like to avoid having to update my routes every time I add an action to a controller.

Is there a shorthand/blurb I can use (and if so, what is it?) which will tell Play to just go ahead and route to all of a given set of (not known in advance) controllers and actions?

1
There isn't to my knowledge. Is it really that hard to add one line to the routes file? If you don't to add them incrementally, define your actions ahead of time with method stubs like def someControllerFunction(param: String, param2: String ....) = TODO. This way you can define your routes ahead of time, and fill in the controller functions later. - Michael Zajac
philosophically, it's a waste of my time. and it's a waste of my time that's likely to happen hundreds, if not thousands, of times over. life is precious. i don't want to waste my life writing lines of code i don't have to :) practically - this is a common, and basic, feature of most routing systems. i'm surprised play doesn't have it - blueberryfields
Reflection seems the only way to go for now. Have you been able to achieve this? I am writting code right now to make it work with reflection. This should be helpful for scala stackoverflow.com/a/1470190/5967859 Will post the answer once I am done. - Saurabh Harwande

1 Answers

0
votes
edit(editType: String) = Action {
implicit request =>
    editType match {
      case "name" =>
        try {
              Ok(views.html.editNamePage())

        } catch {
          case e: Exception =>
            Ok(views.html.errorPage())
        }
      case "saveName" =>
        try {
                Ok("saveFullName")
        } catch {
          case e: Exception =>
            Ok("0")
        }

      case "email" =>
        try {
          Ok(views.html.editEmailpage("result"))
        } catch {
          case e: Exception =>
            Ok(views.html.errorPage())
        }
      case "saveEmail" =>
        try {
              Ok("success")
        } catch {
          case e: Exception =>
            Ok("0")
        }
      case "removeEmail" =>
        try {
            Ok("success")
        } catch {
          case e: Exception =>
            Ok("0")
        }
      }
       }

ROUTE

POST   /edit             controllers.ControllerName.edit(editType:String)

you can use pattern matching on a controller method. by this way you can use a single root multiple times for diffrent purposes. it reduces you number of routes