Christopher wrote:One benefit of MVC with a Front Controller (in PHP) is that it only loads the code you need. The Front Controller uses the route (and other HTTP parameters) to determine which Controller to load. The Controller usually loads the View which uses Models to generate the web page. That provides a structured alternative to old way where every URL is a different PHP script.
It's same in pretty much all frameworks. I do know about MVC pattern.
Christopher wrote:
Because loading some dependencies are part of this design, doing the loading with DI is just an extra layer. Some micro-frameworks do have all the dependency wiring in one place. But that is because they intended for smaller apps.
That's the question I'm not getting.
My current approach for loading dependencies is like this:
Code: Select all
class EngineModel
{
var $database, $bbcode, $filter;
function __construct($config)
{
$this->database = $this->createDatabase($config['DB_NAME']);
$this->bbcode = $this->createBBCode();
$this->filter = $this->createFilter();
$controller = $_GET['page'];
$controller = new Controller($this);
$controller->Start();
}
function createDatabase($config)
{
require_once('library/database.php');
return new Database($config['DB_NAME']);
}
function others() { }
}
class Controller
{
function __construct() { }
function Start()
{
$this->enginemodel->filter->filterBasic($_POST);
$this->enginemodel->database->query($query);
}
}
Something like this. EngineModel loads every library class in construct, even if the controller doesn't need any of those.
But, I want a DI/DI Container approach, like this;
Code: Select all
//load register controller
$this->controller = DependencyContainer::Load('register'); //instead of new Controller();
function Load($objectName)
{
return new $objectName(new Database(), new Recaptcha(), new Filter()); //register controller's dependencies are only 3
}
I don't know if it is a valid DI/DI Container approach. That's basically what I'm asking. I just want to get things done a bit more professionally and learn high level techniques like DI/DI Containers by practise.