I have this type of situation, I have controllers , for example AuthController with functions register and login, I have service called AuthService, here I implement functions where I have all type of logic and communication with database. all I do in controller is $auth_service->register($request) and then Auth service does everything, returns response object and then in controller I return this object as Json. so i have no eloquent functions in controller, no business logic just 3-4 line of code. Is that a good practice? for example if I have need only Modell:all() function in controller I have to use service function even for that little piece of code, otherwise I will have structure where I sometimes use services and sometimes not. Also when I inject service class in controller, I also inject all useful models (sometimes other services) in this service too, if in one controller sometimes I use service and sometimes work with models directly, then turns out that I inject model in service and in controller (injecting same model two times). so should I continue doing like that and use service fore each controllers each function? I mean which way will be better and why?
this is my auth controller's register function:
protected $auth_service;
public function __construct(AuthService $auth_service){
$this->auth_service = $auth_service;
}
public function registerUser(UserRegisterRequest $request){
$registerUser = $this->auth_service->registerUser($request);
return response()->json($registerUser, 200);
}
and this is language controller:
protected $language;
public function __construct(Language $language){
$this->language = $language;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$languages = $this->language->getAllLanguage();
return response()->json(['data'=>$languages], 200);
}
in second example I use model's function directly from controller, but if I choose that first example is better, I should make language service and use it in language controller's index method even though it's quite simple and doesn't need any simplification, so what do you think?
Well if you create service - then I would say it's no longer MVC, because you have one additional layer. I would say that the right way would be to hold these lines in controller.
Symfony framework for example is no longer MVC, because creating services which communicate with data-base and then injecting them in controller is the traditional way of working at the moment.
When can these services be beneficial?
Well consider you have multiple controllers for which you want to share the business logic. Then you just create service and inject this service in multiple controllers.
How can you reuse logic with using pure MVC?
Well, you could just call the controller method from another controller method.
What do I prefer?
As I work mainly with Symfony, I really like this additional service layer, because