Skip to content

Namespace For Controllers, Domains, Plugins and URL Mappings

jeffbrown edited this page May 8, 2012 · 6 revisions

Controllers

A limitation in Grails is the fact that certain artifacts must have unique names, even if they are in separate packages. For example an application may not contain both com.demo.UserController and com.demo.admin.UserController. One of the benefits of the new namespace support will be the ability to allow for multiple artifacts, like controllers, to have the same name as they will be associated with different namespaces.

Admin User Controller

// grails-app/controllers/com/demo/admin/UserController.groovy
package com.demo.admin

class UserController {
    static namespace = 'admin'
    // ...
}

Config User Controller

// grails-app/controllers/com/demo/config/UserController.groovy
package com.demo.config

class UserController {
    static namespace = 'config'
    // ...
}

Other User Controller

// grails-app/controllers/com/demo/UserController.groovy
package com.demo

class UserController {
    // ...
}

URL Mappings

Special support will be added to URL Mappings for simple handling of mapping namespaces to URLs.

// grails-app/conf/UrlMappings.groovy
class UrlMappings {

    static mappings = {
        "/$namespace/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }
    }
}
URL Controller
/admin/user com.demo.admin.UserController
/admin/user/delete/4/ com.demo.admin.UserController
/user com.demo.UserController
/user/delete/4 com.demo.UserController
/config/user com.demo.config.UserController
/config/user/delete/4 com.demo.config.UserController

Domain Classes

TBD

Plugins

Some technique will be provided at the application level to allow a plugin to be associated with a particular namespace, probably some syntax in BuildConfig.groovy. This would mean that all namespace aware artifacts provided by the plugin which do not explicitly express their own namespace are treated as if they were defined in that namespace. In general it will probably make sense for plugins to not express a namespace for their own artifacts and to leave that to the application. If that turns out to be correct, maybe we shouldn't even allow plugin artifacts to express their own namespaces.

Namespace By Convention

We may end up with some mechanism for associating package names with namespaces. This would allow for something like including all controllers in the com.demo.admin package to be in the admin namespace, without having to express that in every controller.

Clone this wiki locally