thesimons's avatar

Sharing private function among different controllers

Hello,

what's the best way to share a private function among different controllers?

I have an area of my about Working Hours with partners: always, everyday (same hours for every day), custom. I have split the logic in 3 different controllers. However there are 3 private functions to be used in each controller.

What's the best approach to share them?

Thanks, Simon

0 likes
3 replies
Glukinho's avatar

Can you show code?

What is "to share" - expose private method to another class? Or to external user?

Snapey's avatar

Traits allow you to share functions so that they are maintained in one place but easily included in other classes.

However, if this is common business logic, you might be better creating a service class, and cutting it out if your controllers altogether.

1 like
june23's avatar

The best way to handle your question is to create Service classes for your Controller classes, and use the private functions within the Service class in the Service classes public functions creating the business logic and then share the public functions that hold the private functions to be used in other Controller classes. That would be the best way to share private functions within the Controllers.

Please or to participate in this conversation.