Grails UrlMapping Redirect to keep DRY
Asked Answered
H

3

9

I am working with Grails 2.1.1 and would like to add a handful of customized URLs that map to Controller Actions.

I can do that, but the original mapping still works.

For example, I created a mapping add-property-to-directory in my UrlMappings as follows:

class UrlMappings {

    static mappings = {
        "/add-property-to-directory"(controller: "property", action: "create")
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

Now, I can hit /mysite/add-property-to-directory and it will execute PropertyController.create, as I would expect.

However, I can still hit /mysite/property/create, and it will execute the same PropertyController.create method.

In the spirit of DRY, I would like to do a 301 Redirect from /mysite/property/create to /mysite/add-property-to-directory.

I could not find a way to do this in UrlMappings.groovy. Does anyone know of a way I can accomplish this in Grails?

Thank you very much!

UPDATE

Here is the solution that I implemented, based on Tom's answer:

UrlMappings.groovy

class UrlMappings {

    static mappings = {

        "/add-property-to-directory"(controller: "property", action: "create")
        "/property/create" {
            controller = "redirect"
            destination = "/add-property-to-directory"
        }


        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

RedirectController.groovy

class RedirectController {

    def index() {
        redirect(url: params.destination, permanent: true)
    }
}
Handmaiden answered 27/9, 2012 at 2:14 Comment(3)
Its not possible as of now. There's a feature request for making it possible to specify redirects in url mappings - See jira.grails.org/browse/GRAILS-5994Snuffle
@sudhir Thank you, that answers my question. Could you please copy your comment to an Answer so I can accept it?Handmaiden
@sudhir, thanks for your comment and the useful link ... Tom edited his Answer and it led me on the right track to do what was looking for so I accepted his answer.Handmaiden
C
4

It is possible to achieve this:

"/$controller/$action?/$id?" (
    controller: 'myRedirectControlller', action: 'myRedirectAction', params:[ controller: $controller, action: $action, id: $id ]
)

"/user/list" ( controller:'user', action:'list' )

and in the action you get the values normallny in params:

log.trace 'myRedirectController.myRedirectAction: ' + params.controller + ', ' + params.action + ', ' + params.id
Conner answered 27/9, 2012 at 8:41 Comment(5)
Thanks for the answer, but I cannot change the main mapping to a redirect controller because I have other Controllers that do follow the Grails convention of controller/action/id. I was hoping to be able to do explicit Redirects.Handmaiden
If you have only small ammount of controller with standard mapping, you can prepare additional specific rules. I have updated my comment to include one.Conner
Thanks, Tom. I accepted your answer, it led me on the right path. I actually ended up creating the RedirectController, but instead of mapping /$controller/$action?/$id?" to it, I created an entry for property/create` to it, and will do so for each URL that I do not want repeated.Handmaiden
Thanks a lot, but honestly I don't fully understand your final solution. You have more "patterns" mapped to the RedirectController?Conner
Good point ... I edited my original question with the implemented solution. Please see Updated section.Handmaiden
S
2

As of Grails 2.3, it is possible to do redirects directly in the UrlMappings, without the need for a redirect controller. So in case you ever upgrade, you can redirect in UrlMappings like so, as per the documentation:

"/property/create"(redirect: '/add-property-to-directory')

Request parameters that were part of the original request will be included in the redirect.

Snapshot answered 31/7, 2017 at 16:51 Comment(0)
K
2

In recent Grails version (currently 5.x) you need to pass a Map into the redirect: property:

"/viewBooks"(redirect: [uri: '/add-property-to-directory'])

https://docs.grails.org/latest/guide/theWebLayer.html#redirectMappings

Kishakishinev answered 4/10, 2022 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.