Search code examples
grailsurl-mapping

put namespace in url


grails 2.3.8 here. When i g:link to certain controller actions, i want to put a namespace in the url in front of the controller name.

For example: call "app/apple/eat" -> "app/admin/apple/eat"

Since there are many dynamic and static scaffolded controllers involved, I thought i can do this with some UrlMappings expression but i dont know how to do this.

I tried out something like this without no success:

static mappings = {
    "/apple/$action?/$id?" (redirect:"/admin/apple/$action?/$id?")
}

using namespace = "admin" in the AppleController doesnt work aswell

thanks for advices


Solution

  • For the example

    call "app/apple/eat" -> "app/admin/apple/eat"
    

    you could do it via the grails-app/conf/UrlMappings.groovy with the following configuration (which works than for the whole application):

    class UrlMappings {
    
        static mappings = {
            "/admin/$controller/$action?/$id?(.$format)?"{
                constraints {
                    // apply constraints here
                }
            }
    
            "/"(view:"/index")
            "500"(view:'/error')
        }
    }
    

    with this configuration, your controllers are reachable via:

    http://localhost:8080/apple/admin/apple/eat/1
    
    etc.
    

    Or!

    If you would only have apple under the admin prefix, than you could do the following in grails-app/conf/UrlMappings.groovy:

    class UrlMappings {
    
        static mappings = {
            "/admin/apple/$action?/$id?"(controller : "apple")
            "/$controller/$action?/$id?(.$format)?"{
                constraints {
                    // apply constraints here
                }
            }
    
            "/"(view:"/index")
            "500"(view:'/error')
        }
    }
    

    Or!

    If you have more redirects, you could group them together:

    static mappings = {
        group("/admin") {
            "/apple/$action?/$id?(.${format})?"(controller: 'apple')
            "/peach/$action?/$id?(.${format})?"(controller: 'peach')
        }
        ...