Primus
Primus

Reputation: 275

Finding all controllers in application

How do i find all the controllers running in an application ?

I am trying to create a menu using YUI where only registered controllers will have a menu shown. A controller class will create a static list with various properties detailing name, action, etc. (much like grails-nav plugin).

I want to create a taglib that can find all controllers, identify which ones have this static list then look into each list and build up a menu.

I think i can use ControllerGrailsClass.metaClass.hasProperty to identify whether a given controller has the static property - but how do I find all the Controller classes to interrogate ?

Thanks in advance

Upvotes: 6

Views: 4583

Answers (1)

ataylor
ataylor

Reputation: 66069

You can get a list from the GrailsApplication object. Example:

class TestController {

    def grailsApplication // gets injected automatically

    def test = {
        grailsApplication.controllerClasses.each { controllerArtefact ->
            def controllerClass = controllerArtefact.getClazz()
            println "$controllerArtefact, $controllerClass"
        }
    }
}

If you're not in a controller, you can get a hold of the grails application object like so:

import org.codehaus.groovy.grails.commons.ApplicationHolder

def grailsApplication = ApplicationHolder.application

Upvotes: 10

Related Questions