Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Mapping prefix in Grails

Recently, I'm trying to migrating my application from CakePHP to Grails. So far it's been a smooth sailing, everything I can do with CakePHP, I can do it with much less code in Grails. However, I have one question :

In CakePHP, there's an URL Prefix feature that enables you to give prefix to a certain action url, for example, if I have these actions in my controller :

PostController
admin_add
admin_edit
admin_delete

I can simply access it from the URL :

mysite/admin/post/add
mysite/admin/post/edit/1
mysite/admin/post/delete/2

instead of:

mysite/post/admin_add
mysite/post/admin_edit/1
mysite/post/admin_delete/2

Is there anyway to do this in Grails, or at least alternative of doing this?

like image 973
Furunomoe Avatar asked Jun 17 '10 04:06

Furunomoe


3 Answers

Grails URL Mappings documentation doesn't help you in this particular case (amra, next time try it yourself and post an answer only if it's any help). Daniel's solution was close, but wouldn't work, because:

  1. the action part must be in a closure when created dynamically
  2. all named parameters excluding "controller", "action" and "id" are accessible via the params object

A solution could look like this:

    "/admin/$controller/$adminAction?/$param?"{
    action = { "admin_${params.adminAction}" }
}

The key is NOT to name the parameter as "action", because it seems to be directly mapped to an action and can not be overriden.

I also tried a dynamic solution with generic prefixes and it seems to work as well:

    "/$prefix/$controller/$adminAction?/$param?"{
    action = { "${params.prefix}_${params.adminAction}" }
}
like image 75
Tomas Zilvar Avatar answered Nov 15 '22 05:11

Tomas Zilvar


I didn't test it, but try this:

"mysite/$prefix/$controller/$method/$id?"{
    action = "${prefix}_${method}"
}

It constructs the action name from the prefix and the method.

like image 40
Daniel Engmann Avatar answered Nov 15 '22 03:11

Daniel Engmann


Just take a look on grails URL Mappings documentation part

like image 37
amra Avatar answered Nov 15 '22 03:11

amra