Laravel Türkiye Discord Kanalı Forumda kod paylaşılırken dikkat edilmesi gerekenler!Birlikte proje geliştirmek ister misiniz?

Merhaba arkadaşlar Controllerde her kes için olmasada çoğumuz __construct method kullanmışızdır lakin proje büyüdükce extend etdiğimiz classlarda bu __construct methodları tekrar yazmamız gerekiyor

class ApiController extends Controller
{
    public function __construct(FileService $fileService)
    {
        $this->file = $fileService;
    }

}

class UserController extends ApiController
{
    public function __construct(FileService $fileService, UserRepository $repository)
    {
        parent::__construct($fileService);
        $this->repository = $repository;
        $this->middleware('auth');
    }
}

böyle bir örnekde düzensiz ve gereksiz yazılan kodlar olduğunu kabul edelim şimdi bunu düzeltelim

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->resolving(\Illuminate\Routing\Controller::class, static function ($object, $app) {
            foreach (get_class_methods($object) as $method) {
                if (Str::startsWith('boot', $method)) {
                    $app->call([$object, $method]);
                }
            }
        });
    }
}

artık base controllerden türeyen her class container tarafından oluşturulduğunda bu kod çalışacaktır
controllerin yeni haline bakalım

class ApiController extends Controller
{
    public function bootFileService(FileService $fileService)
    {
        $this->file = $fileService;
    }
}

class UserController extends ApiController
{
    public function bootUserRepository(UserRepository $repository)
    {
        $this->repository = $repository;
    }
    
    public function bootMiddleware()
    {
        $this->middleware('auth');
    }
}