Skip to content

Namespace For Controllers, Domains, Plugins and URL Mappings

jeffbrown edited this page May 8, 2012 · 6 revisions

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 {
    // ...
}

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

URL Mapping

// 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
Clone this wiki locally