Reputation: 654
I have the need for a reusable controller in my project, but I would like to configure slightly depending on the usage. I tried using default parameters in the action method like this:
object SimplePage extends Controller {
def index(pageTitle: String,
baseUrl: String = "") = Action {
routing it like this:
GET / controllers.SimplePage.index(pageTitle="Test Page")
GET /subdir controllers.SimplePage.index(pageTitle="Test Page 2", baseUrl = "foo")
but I get an error like this:
Error:(5, -1) Play 2 Compiler: Compilation error[Using different overloaded methods is not allowed. If you are using a single method in combination with default parameters, make sure you declare them all explicitly.]
I simplified this a bit. In reality, there are 5 different parameters, with reasonable defaults. It looks like it would work if I just specified all 5 values for each usage, but that's just silly. Most of the time, we will only need to set one or two parameters (if any).
Are there any better ways 'configure' reusable controllers like this?
Upvotes: 0
Views: 1172
Reputation: 13749
I think you have to set all the default parameters in your route
file using ?=
syntax.
GET / controllers.SimplePage.index(pageTitle ?= "Test Page", baseUrl ?= "")
GET /subdir controllers.SimplePage.index(pageTitle ?= "Test Page 2", baseUrl ?= "foo")
Upvotes: 1