tags:

views:

27

answers:

2

Is it possible to configure grails to resolve controllers and actions using the package they are in as sub-folders?

For example, say I have the following directory structure:

/grails-app/controllers/HomeController.groovy (with action index)
/grails-app/controllers/security/UserController.groovy (with actions index, edit)
/grails-app/controllers/security/RoleController.groovy (with action index, edit)

I would like grails to generate the following url mappings automatically:

http://localhost:8080/myApp/ => HomeController.index
http://localhost:8080/myApp/security/user/ => UserController.index
http://localhost:8080/myApp/security/user/edit => UserController.edit
http://localhost:8080/myApp/security/role/ => RoleController.index
http://localhost:8080/myApp/security/role/edit => RoleController.edit
A: 

I believe you are looking for Grails URLMapping constraint. Look here: Grails - URL mapping

John Engelman
A: 

I would be a bit wary of mapping them directly to your package names. Doing that will make it very hard for you to refactor in the future, especially once your application is out in the wild. It's also going against the conventional nature of Grails.

However, you can still manually structure your URLs to map to different paths. For your question, here's an example:

// grails-app/conf/UrlMappings.groovy

'/security/user/$action?/$id?' (controller: 'user')
'/security/role/$action?/$id?' (controller: 'role')

// the rest of the default UrlMappings are below
'/$controller/$action?/$id?' {}

Since controllers are typically referenced by name, e.g. "user" in your case, it's not easy to go against this convention; it'd be trying to fight the framework instead of letting it do the work for you.

It's probably possible to do this based on package (maybe using Reflection, or something?), but not advisable.

Rob Hruska
I see the point of not wanting to map them directly in case you need to refactor your code later. But since controllers are supposed to be pretty dumb anyway, they shouldn't really need refactoring. Plus, the times that I want to refactor the controllers I am usually changing the views along with them anyway. Primarily my goal in doing this is to make it easier for the other developers on the team to figure out what controllers belong to what actions.
Blacktiger
@Blacktiger - I will agree that controllers shouldn't really be complicated things. However, I think you might be overthinking your problem here. It should be easy for developers to find out which controller/action they need simply by looking at all of the controller names. By putting them into separate packages, you're probably actually making it *harder* to find them since developers have to look through several directories in the tree just to see the controller names.
Rob Hruska
@Blacktiger - I probably didn't emphasize it enough in my answer, but what I'm ultimately trying to say is that using the package names works against what Grails is trying to do for you out of convention.
Rob Hruska