I have built a few MVC web applications now and have been adapting my developments to better suit the best practices, I am now working on another project and have applied all previous knowledge I have gained however I have a few questions on how would the best way to implement dependency injection into my project be.
I have an index.php which looks something along the lines of:
require __DIR__ . '/../vendor/autoload.php';
use Phroute\Phroute\{RouteCollector, Dispatcher};
require __DIR__ . '/../routes.php';
...
Now I did try Pimple however I'm not sure if this is the best option for me or if there are other better options such as aura.
I defined all of the dependencies in the index file such as my database etc, then I wasn't sure on the best method to use these within my controllers. I did think about this method, but it does feel that it would be very unconveniant to repeat myself by throwing the container into every controller's constructor, is this a bad thing to do; if so how can I avoid it so that the container would be kind of "autoloaded" with every controller?
namespace App\Controllers;
#use Pimple\Container;
class Index {
public function __construct(Pimple\Container $container){
$this->container = $container;
}
public function index(){
return $this->container['db'];
// ...
}
}