Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework CRUD

I wanted to develop a RESTful Application with use of CRUD in Play framework. Unfortunately I can't find a way to define DELETE and PUT in the routes of Play. Maybe there is just POST and GET available in Play?

like image 691
Ghashange Avatar asked Dec 12 '12 18:12

Ghashange


3 Answers

Are you sure you cannot use DELETE/PUT? The docs say otherwise.

The HTTP method

The HTTP method can be any of the valid methods supported by HTTP (GET, POST, PUT, DELETE, HEAD).

http://www.playframework.org/documentation/2.0.4/JavaRouting

like image 56
atamanroman Avatar answered Sep 21 '22 17:09

atamanroman


Play 2.x has not a CRUD module known from 1.x branch (IMHO fortunately), for defining routes using not standard methods like DELETE or PUT you need to just use required method in the route:

conf/routes:

PUT     /put-item     controllers.Application.putItem()

Anyway to use them from the browser methods other than GET or POST you'll need to create an AJAX call, There is a large step-by-step sample on this topic, anyway you can also build it with common jQuery.ajax() by defining the request type

$.ajax({
  type: "PUT",
  url: "@routes.Application.putItem()",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});
like image 41
biesior Avatar answered Sep 21 '22 17:09

biesior


A good way to define these is using a wildcard (*) This will allow you to use any method valid http method, including those that you have asked about.

For example,

*  /items/{id}               Items.display

in routes will allow a GET /items/15 or a PUT /items/15. Use wildcards like this to make your route definitions simpler and more flexible.

like image 24
Slayer6 Avatar answered Sep 18 '22 17:09

Slayer6