Struggling with MVC in PHP. My concerns grew even bigger after watching this: https://www.youtube.com/watch?v=RlfLCWKxHJ0
According to LoD my Router class should only know the Request Uri to load proper Controller class. However my Conroller should know which Model class it should use and a View class to present data. Or better, Controller should know ModelFactory that will handle object creation using selected data storage.
This all breaks LoD to me.
So my question is:
- How Router should init Controller class not knowing which parameters it needs? Even if it is DI container, we don't know object parameters to be passed to constructor inside Router. If we pass DI container to Router constructor (or any other class) we get back to Service Locator. How should this be done?
Maybe this is all wrong, but my starting point is:
// ... retrieve settings, available languages, start session,...
$router = new Router($settings);
$router->loadController();
Router.php
class Router
{
public function __construct(Settings $settings)
{
$this->settings = $settings;
}
// some other methods
public function loadController()
{
try
{
// Loading controller
$controller = $this->getController();
if (is_callable(array($controller, $this->method)) == false)
$this->method = 'init';
// Running controller
$controller->{$this->method}();
}
catch (Exception $e)
{
$e->displayMessage();
}
}
}
And from here on I can do nothing inside my Controller class, cause I need to call new Model, new View and have to do it explicitly inside constructor or method, which is bad.
More Questions:
- How should I get instance of Model class in Controller? Should I use static method to load View?