Laravel 4 - Child constructor call parent constructor with dependency injection

后端 未结 6 1858
死守一世寂寞
死守一世寂寞 2021-02-05 02:53

I\'m building a CMS using Laravel 4 and I have a base admin controller for the admin pages that looks something like this:

class AdminController extends BaseCont         


        
6条回答
  •  故里飘歌
    2021-02-05 03:22

    I came across the same issue, when extending my base Controller.

    I opted for a different approach than the other solutions shown here. Rather than rely on dependency injection, I'm using app()->make() in the parents constructor.

    class Controller
    {
        public function __construct()
        {
            $images = app()->make(Images::class);
        }
    }
    

    There may be downsides to this simpler approach - possibly making the code less testable.

提交回复
热议问题