Reputation: 1744
Question.
I have a bunch of controllers that are using a certain set of services. I was wondering if it is possible / right to utilize inheritance to save me from having to inject them into controllers all the time. This is what I was planning on doing.
class MasterController extends controller{
public function _construct(){
$this->userData = App::make(UserService::class)
$this->fooData = App::make(FooService::class)
}
}
class UserController extends MasterController {
public function __construct(BashService $bashService){
parent::__construct();
$this->bashData = $bashService;
}
public function someFunction(){
$something = $this->userData->doUserSomething();
}
}
Is this a good idea to do? A really bad idea to do? Why or why not? I thought this might save me from having to inject common services again and again into controllers.
Thanks!
Upvotes: 5
Views: 470
Reputation: 724
Question:
I was wondering if it is possible/right to utilize inheritance to save me from having to inject them into controllers all the time.
Answer:
Question:
Is this a good idea to do?
Answer:
Question:
Why or why not?
Answer:
Since I have explained most of the part, I would further like to go with to suggest you the repository pattern so basically by the word repository I mean this :
In laravel you can follow repository pattern to code your project which is currently found the best practice.
You can do a google search too on laravel repository pattern.
I have an example which will make you understand this.
Upvotes: 2