0

I want to be able to create global functions, meaning a function I can use across controllers, kind of like helper methods.

So in one controller I could do

useful_function(string) etc... Is this possible?

I did create a class in src/groovy called SiteHelper, am I on the right track? I want the methods of the class SiteHelper to be able to be used throughout controllers.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Ron
  • 513
  • 1
  • 4
  • 22
  • You can use a groovy category to create this kind of globally accessible function. See this question: http://stackoverflow.com/questions/4195492/how-do-you-share-common-methods-in-different-grails-controllers – ataylor Feb 04 '13 at 22:58

4 Answers4

4

Yes, you're mostly on the right track. You may want to look at making it as a part of service layer.

http://grails.org/doc/latest/guide/services.html

vector
  • 7,334
  • 8
  • 52
  • 80
  • I've just completed my first service in Grails, an auth service, this might be more ideal, thanks – Ron Feb 04 '13 at 20:09
3

You can add it to the metaclass of all controller classes, for example in BootStrap.groovy:

class BootStrap {

   def grailsApplication

   def init = { servletContext ->
      for (cc in grailsApplication.controllerClasses) {
         cc.clazz.metaClass.useful_function = { String s ->
            return ...
         }
      }
   }
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
0

The standard way to share logic between different components in Grails is to put it in a service, or alternatively in a taglib in the case of functions that need access to web-layer things like request/response/params/session/flash. You can call taglib tags as methods from any controller action:

MyTagLib.groovy

class MyTagLib {
  def sayHello = { attrs, body ->
    out << "Hello ${attrs.name}"
  }
}

MyController.groovy

def someAction() {
  def greeting = sayHello(name:"Ian")
  // ...
}
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
-2

I don't get what is nontrivial about this. It sounds exactly what Apache's StringUtils class or IOUtils class does. Yes, creating a SiteHelper with static methods and importing it will do what you want and is the typical practice for this in Java-influenced (and many other) languages.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Would the downvoter care to explain what's incorrect about this? – djechlin Feb 04 '13 at 20:04
  • I was hoping for a way without importing, possibly autoloading the methods in the controllers, I'm really creating a method called flashmsg() which just flashes a message, which could be useful throughout almost every controller - btw I didn't downvote – Ron Feb 04 '13 at 20:05
  • 1
    Then you need to make your question clearer to explain this is your need - and I don't really understand what's so anathemic about an `import` statement, the short answer is going to be, "no, you need to import something to use it." Anyway it's not good practice to downvote a correct answer because you're unhappy about the language features available. – djechlin Feb 04 '13 at 20:05