Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails - hide the /index Action using Routes

What it is the correct way to hide the /index action using route in a Grails app?

I want to be able redirect to a controller: "profile", action: "index" but have the url look like
http://foobar.com/profile
not
http://foobar.com/profile/index

like image 467
BuddyJoe Avatar asked Aug 02 '11 19:08

BuddyJoe


2 Answers

UrlMappings.groovy

static mappings = {
      "/profile"(controller:"profile", action: "index")
}

Or you could set your default action in the Controller

class BookController {
    static defaultAction = "index"
}

If you are wanting to redirect to that URL then from within an action in a controller..

redirect uri: '/profile'  // This one for the UrlMappings solution

or

redirect controller: 'profile'  // This one for the defaultAction solution.
like image 98
Nick Larson Avatar answered Sep 19 '22 23:09

Nick Larson


"/foo/" (controller:"foo", action:"index")

This goes in UrlMapping.groovy. UrlMapping.groovy works both ways, meaning that a g:link to controller foo and action index will also generate that shortened url.

You would need to do this for every controllers index though.

I tried setting:

"/$controller" {}

But no cigar, maybe someone else knows how this works.

Edit: Hah, I started writing this answer before you Nick, then spent 10 mins testing out various combinations of just the $controller thingie :-)

like image 45
Oliver Tynes Avatar answered Sep 19 '22 23:09

Oliver Tynes