1 month ago

Laravel logical issue

Posted 1 month ago by vlauciani94

Imagine I have 2 controllers, UserController and PostController, however, I want to use a function which has a general use and doesn't thematically belong to any of those classes. Something like:

public function sum($numberOne, $numberTwo) {
    return $numberOne + $numberTwo;

Where am I supposed to create it in order to be able to use it in both UserController and PostController? I constantly read that it's a bad practice to call a controller method from another controller but I just can't figure out what's the alternative.

I could just create the function in each controller but then I'd be repeating myself and the code won't be easily maintainable.

Please sign in or create an account to participate in this conversation.