I have read multiple articles that stated that the correct and most proper way of doing dependency injection is to pass in only the dependencies your controller needs to it, as opposed to the entire container (which actually seems to be very common practice). I have read that passing in the entire container is not recommended, as it means the class will have access to every service in it, which results in the class having hidden dependencies, which is bad.
I read that you should create DI factories and pass in the required dependencies only. Here's what I'm doing:
$container["Auth\AuthController"]=function($container){
$view=$container->view;
$router=$container->router;
$auth=$container->auth;
$validator=$container->validator;
return new Fleet\Controllers\Auth\AuthController($view,$router,$auth,$validator);
};
$container["Auth\LoginController"]=function($container){
$view=$container->view;
$router=$container->router;
$auth=$container->auth;
$validator=$container->validator;
return new Fleet\Controllers\Auth\LoginController($view,$router,$auth,$validator);
};
$container["HomeController"]=function($container){
$view=$container->view;
$router=$container->router;
return new Fleet\Controllers\HomeController($view,$router);
};
$container["UserController"]=function($container){
$view=$container->view;
$router=$container->router;
return new Fleet\Controllers\UserController($view,$router);
};
$container["VehicleController"]=function($container){
$view=$container->view;
$router=$container->router;
return new Fleet\Controllers\VehicleController($view,$router);
};
Am I doing it correctly? Is there a better way of doing it without having all this pretty much repetitive code?