Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable Grails controller helper methods

Tags:

grails

How to create reusable Grails controller helper methods which can be used in many controllers?

Right not I have few private methods in one controller. I want to share them with other controllers.

I would like have access to params, redirect etc.

like image 816
Michal Z m u d a Avatar asked Mar 20 '23 14:03

Michal Z m u d a


1 Answers

The correct way to share code between controllers is to abstract the logic into a service. See

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

Note that if the service is not required to be transactional you should mark it as such.

If however you have web related logic (such as writing templates or markup to the output stream) then you can also use tag libraries to share logic, as tags can be invoked from controllers. See:

http://grails.org/doc/latest/guide/theWebLayer.html#tagsAsMethodCalls

like image 79
Graeme Rocher Avatar answered Apr 06 '23 22:04

Graeme Rocher