2
votes

I have a search function in a Play 2.1.4 project that routes to /search/:query.

How would I go about in handling a situation where a user searches without adding a query? Optional parameters are gone since Play 2.1, and I do not believe using a different route for it is a pretty solution.

1

1 Answers

3
votes

Actually what's wrong about two routes? From logical point of view these are two separate actions: route with param should initialize the search process, while empty one should show empty search form or do some redirect.

GET     /search/:term   controllers.Application.search(term: String)
GET     /search         controllers.Application.blankSearch

Optionaly you can use a route with default value (ie. empty String) which is clean approach as common HTML form will send the request in exactly required format:

GET     /search         controllers.Application.search(q: String ?= "") 

(request: http://domain.loc/search?q=Looking+for+something)