jeudi 28 juin 2018

Laravel Common Services Through Inheritance

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!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire