Search

Dark theme | Light theme
Showing posts with label GrailsGoodness:URLMapping. Show all posts
Showing posts with label GrailsGoodness:URLMapping. Show all posts

November 18, 2013

Grails Goodness: Grouping URL Mappings

We can group URL mappings defined in grails-app/conf/UrlMappings.groovy using the group() method defined for the URL mapping DSL. The first argument is the first part of the URL followed by a closure in which we define mappings like we are used to.

Suppose we have defined the following two mappings in our UrlMappings.groovy file, both starting with /admin:

// File: grails-app/conf/UrlMappings.groovy
class UrlMappings {

    static mappings = {
        // Mappings starting both with /admin:
        "/admin/report/$action?/$id?(.${format})?"(controller: 'report')
        "/admin/users/$action?/$id?(.${format})?"(controller: 'userAdmin')

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

We can rewrite this and use the group() method to get the following definition:

// File: grails-app/conf/UrlMappings.groovy
class UrlMappings {

    static mappings = {
        // Using grouping for mappings starting with /admin:
        group("/admin") {
            "/report/$action?/$id?(.${format})?"(controller: 'report')
            "/users/$action?/$id?(.${format})?"(controller: 'userAdmin')
        }

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

When we use the createLink and link tags the group is taken into account. For example when we use <g:createLink controller="userAdmin"/> we get the following URL if the application name is sample: /sample/admin/users.

Code written with Grails 2.3.

Grails Goodness: Create Report of URL Mappings

Since Grails 2.3 we can use the url-mappings-report command to get a nice report of the URL mappings we have defined in our application. Also implicit mappings created for example by using the resources attribute on a mapping definition are shown in the report. This report is very useful to see which URLs are exposed by your application and how they map to controllers.

Suppose we have the following grails-app/conf/UrlMappings.groovy with a couple of mappings:

// File: grails-app/conf/UrlMappings.groovy
class UrlMappings {

    static mappings = {
        // Map to HTTP methods.
        "/upload"(controller: 'upload') {
            action = [POST: 'file']
        }

        // RESTful API.
        "/api/users"(resources: 'user')

        // Default mapping.
        "/$controller/$action?/$id?(.${format})?"()

        // Main index.
        "/"(view: "/index")

        // Error mappings.
        "500"(controller: 'error')
        "404"(controller: 'error', action: 'notFound')
    }
}

When we run the following command $ grails url-mappings-report we get the following output:

| URL Mappings Configured for Application
| ---------------------------------------

Dynamic Mappings
 |    *     | /${controller}/${action}?/${id}?(.${format)?              | Action: (default action)      |
 |    *     | /                                                         | View:   /index                |

Controller: dbdoc
 |    *     | /dbdoc/${section}?/${filename}?/${table}?/${column}?      | Action: (default action)      |

Controller: error
 |    *     | ERROR: 500                                                | Action: (default action)      |
 |    *     | ERROR: 404                                                | Action: notFound              |

Controller: upload
 |    *     | /upload                                                   | Action: {POST=file}           |

Controller: user
 |   GET    | /api/users                                                | Action: index                 |
 |   GET    | /api/users/create                                         | Action: create                |
 |   POST   | /api/users                                                | Action: save                  |
 |   GET    | /api/users/${id}                                          | Action: show                  |
 |   GET    | /api/users/${id}/edit                                     | Action: edit                  |
 |   PUT    | /api/users/${id}                                          | Action: update                |
 |  DELETE  | /api/users/${id}                                          | Action: delete                |

Notice also mappings added by plugins like the mappings to dbdoc controller are shown.

Code written with Grails 2.3.