1

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?

blueberryfields
  • 45,910
  • 28
  • 89
  • 168
  • 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 Mar 01 '14 at 15:20
  • 1
    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 Mar 03 '14 at 03:46
  • 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 https://stackoverflow.com/a/1470190/5967859 Will post the answer once I am done. – Saurabh Harwande Jan 20 '18 at 01:40

1 Answers1

0
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

Govind Singh
  • 15,282
  • 14
  • 72
  • 106
  • ok, so instead of adding it to a route, i add it to the controller; not just that, but i'm writing even more lines of, more complex, code. i've been reading - maybe there's a way to do this with regexps? but i don't see how to specify a wildcard target – blueberryfields Feb 27 '14 at 14:32
  • Please don't ever do this. The above code will cause match errors, is horribly verbose, unreadable, and an utter abuse of the scala language. Enough match/cases will probably also cause a stack overflow. – Michael Zajac Mar 02 '14 at 00:58