I would like to ask you about SOLID principles in Laravel, actually it's question about single responsibility principle.
Let's assume I have ExcelController class where I import users to database. I also have UserController where I have createUser method. The problem is that ExcelController cannot extend from UserController, because it already extends from Controller. So in this case should I create UserTrait and there put the createUser method?
What's the best approach?
I'd rather suggest you create service User
and have all methods inside this service. And from ExcelController
call:
(new UserService())->import($parameters);
And from UserController
call:
(new UserService())->create($parameters);
In laravel you can use Service Container
and write something like:
$userService = $this->app->make('UserService');
$userService->import($parameters);
// or
$userService->create($parameters);