Dynamic Controller Plugin

  • Tags : /
  • Latest : 0.5.1
  • Last Updated: 20 March 2014
  • Grails version : 2.0 > *
3 votes

3% of Grails users

Dependency :
compile ":dynamic-controller:0.5.1"

Documentation Source Issues

Summary

Supports controller mixins, where action closures are retrieved from various sources including existing controllers, files, database source, etc. Can also create full controllers dynamically.

Installation

grails install-plugin dynamic-controller

Description

See http://burtbeckwith.com/blog/?p=1041 for a writeup of the plugin's features.

This plugin allows you to dynamically create controllers and add actions to existing controllers. The actions can be "copied" from existing controllers (e.g. instead of using url mappings), defined in a ControllerMixin (a new Artifact defined by the plugin), from a database, or any custom location you like as long as you implement the ClosureSource interface.

When you install the plugin, it creates the grails-app/controllerMixins folder. Controller mixins look just like controllers, but they only exist to be mixed into real controllers. You can use these to partition controllers into individual files, or create an initial controller that one or more plugins extend with their own mixins. A ControllerMixin must have a class name matching *ControllerMixin.

You can define the destination controller for a controller mixin in one of two ways. One is a static controller property which specifies the class name of the controller to mix into. The other way is an attribute in Config.groovy, grails.plugins.dynamicController.mixins. A mixin doesn't affect the code or even bytecode - everything is done via Grails url registration, so really all that's being done is that in addition to existing /foo/** actions for FooController, new actions will be added under the /foo/ URL pattern.

For example, suppose we have this simple controller in grails-app/controllers:

package com.yourcompany.yourapp

class FooController {

def someAction = { … } }

and these two mixins in grails-app/controllerMixins :

package com.yourcompany.yourapp

class SomeControllerMixin {

static controller = 'com.yourcompany.yourapp.FooController'

def someMixinAction = { … } }

and

package com.yourcompany.yourapp

class SomeOtherControllerMixin {

def someOtherMixinAction = { … } }

As long as SomeOtherControllerMixin is registered in Config.groovy (since it has no controller property):

grails.plugins.dynamicController.mixins = [
    'com.yourcompany.yourapp.SomeOtherControllerMixin': 'com.yourcompany.yourapp.FooController'
]

then you'll have these URLs available:

  • /foo/someAction
  • /foo/someMixinAction
  • /foo/someOtherMixinAction
In development mode, controller mixins reload just like controllers and other artefacts, but in production mode all dynamic controller actions are cached once they're resolved.

Note that you can even create entire new plugins at runtime. If you specify a destination controller name that doesn't correspond to an existing controller, the plugin will create a new controller and mix the actions into it. So for example if SomeControllerMixin's controller property were com.yourcompany.yourapp.NewController and Config.groovy contained this:

grails.plugins.dynamicController.mixins = [
    'com.yourcompany.yourapp.SomeOtherControllerMixin': 'com.yourcompany.yourapp.NewController'
]

then you would end up with these URLs:

  • /foo/someAction
  • /new/someMixinAction
  • /new/someOtherMixinAction
In addition, since these are real controllers, they can be managed in UrlMappings.groovy