I want to use a function in both a controller and a module. where should I define that function?
Can I call a function in module from a controller? How I can do that?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.Sign up to join this community
First off, a controller is just an instance of a class. As such, calling a function from a controller is not different from what you would do in another PHP class. To answer your questions:
Where should I define that function? You can define it in the module file, since that is already loaded when a controller object is created and its methods are called.
Can I call a function in module from a controller? How I can do that? Yes, you can, and you don't need to do something more than calling the function as you would call it from another function, since modules are already loaded when a controller object is created.
Comparing a single function with a service doesn't make much sense; you will never replace a function like
taxonomy_get_title() with a service. I would rather compare a service with a group of related function, and in that case, I would say that a service is preferable to a group of related functions.