Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Grails supports Restful nested URLs?

Tags:

grails

Does Grails supports Restful nested URLs like '/articles/1/comments/5' by default? If not is there any plugin for that?

like image 731
syllepsa Avatar asked Jul 22 '13 21:07

syllepsa


2 Answers

See here. Nested RESTful URLs will be supported in Grails 2.3.

like image 114
Robert Harvey Avatar answered Oct 20 '22 18:10

Robert Harvey


If you're bound to an older release of Grails (e.g < 2.3), and the available plugins don't work out, you can use named URL mappings to produce an effective restful mapping.

Here's an example from a project of mine - I've left out some details, but hopefully this gets you started if you decide to give this approach a try.

In your UrlMappings.groovy

/** 1. Mappings can handle multiple actions depending on HTTP 
    method like Rest. Names are a little clunky, like this would
    be more appropriate as "resource" vs "showResource" but we didn't want
    potential naming conflict in future release 

    2. TODO: DRY constraints - make constraints global  
    3. make sure controllers have proper actions defined
*/

/** RESTFUL mapping for single resource */
name listResources: "/$controller" { 
  action = [GET: "list", POST: "save"] 
}
name createResource: "/$controller/create" { 
  action = [GET: "create" ] 
}
name deleteResource: "/$controller/$id?/delete" { 
  action = [POST: "delete", DELETE: "delete"] 
  constraints { id(matches: /[0-9]+/) }
}
name editResource: "/$controller/$id?/edit" { 
  action = [GET: "edit", PUT: "update", POST: "update"] 
  constraints { id(matches: /[0-9]+/) }
}
name showResource: "/$controller/$id?" { 
  action = [GET: "show", PUT: "update", POST: "update", DELETE: "delete"]
  constraints { id(matches: /[0-9]+/) }
}

/** RESTFUL mapping for CHILD with PARENT */
name listChildResources: "/$parentResource/$pid/$controller" { 
  action = [GET: "list", POST: "save"]
  constraints { pid(matches: /[0-9]+/) }
}
name createChildResource: "/$parentResource/$pid/$controller/create" {
  action = [GET: "create" ] 
  constraints { pid(matches: /[0-9]+/) }
}
name showChildResource: "/$parentResource/$pid/$controller/$id?" { 
  action = [GET: "show", PUT: "update", POST: "update", DELETE: "delete"] 
  constraints { 
    id(matches: /[0-9]+/)
    pid(matches: /[0-9]+/) 
  }
}
name editChildResource: "/$parentResource/$pid/$controller/$id?/edit" { 
  action = [GET: "edit"]
  constraints { 
    id(matches: /[0-9]+/)
    pid(matches: /[0-9]+/) 
  }
}

Make sure you controllers have actions and supported HTTP methods define, eg

static allowedMethods = [
  save: "POST", 
  update: ["POST", "PUT"], 
  delete: ["POST", "DELETE"]
]

Then use the mappings like so (for example lets say we have Gardens and Plants as resources).

//show a garden
<g:link mapping="showResource" controller="garden" 
   id="${gardenInstance.id}">${gardenInstance.name}</g:link>

//create a plant for garden
<g:link mapping="createChildResource" controller="plant" 
   params="[parentResource: 'garden', pid: gardenInstance.id]">Add Plant</g:link>

//show list of plants within a garden
<g:link mapping="listChildResources" controller="plant" 
   params="[parentResource: 'garden', pid: gardenInstance.id]">List plants for Garden</g:link>

Shown here it's pretty verbose, but you could put all of this into TagLib and have something like.

<g:restShow resource="garden" 
   id="${gardenInstance.id}">${gardenInstance.name}</g:restShow>

<g:restCreate" resource="plant" 
   parent="${gardenInstance}">Add Plant</g:restCreate>
like image 23
ikumen Avatar answered Oct 20 '22 17:10

ikumen