I am looking for a better explanation of the view layer in MCV, and in particular how the flow in the program goes from controller to view, with focus on the state of the model when using a 1:1 relationship between controller and view.
In all examples I have seen data is being forwarded to the view from the controller, and the view does nothing specific that would require a specific view to be written for a specific controller. Have I misunderstood the 1:1 relationship mantra? The latest example I found was posted here some days back: https://stackoverflow.com/a/18983927/1681418
class View
{
public function render($templateFile, array $vars = array())
{
ob_start();
extract($vars);
require($templateFile);
return ob_get_clean();
}
}
I have tried to create specific view classes for each controller, and I currently have a view that extracts all data from the model as it requires it. It is clean in the sense that I have a very defined do-stuff-to-the-model section (=controller) and a read-only-from-model section (=view). I have however a few shortcomings that I have yet to find a tidy solution for, namely:
- Where should the template file be selected?
- How does the view get to know about errors in the model?
- How does the view get to know about successful or unsuccessful command/action in controller?
- How do I change view when there is an error? Or how do I properly do my routing for the cases when a user state changes.
I have no trouble rendering correct output on my page, but my current approach feels wrong. **Anyone got an example of a view that uses domain driven design with the model as a layer, not a class? **
This answer is very similar to what I usually find, and I do not understand how this approach uses or requires a 1:1 relationship.
I am mostly looking for examples, not code review, but I have in any case extracted some pieces of my code below for examples. Here I am calling the controller via a dispatcher for access control and routing, then the view via the same dispatcher to again check for access. The view in turns calls different presentation objects that assigns data to the template engine if http request, json if ajax request.
class Controller
{
public function login()
{
$this->serviceFactory
->build('recognition')
->authenticate($this->request->username, $this->request->password);
}
}
class View
{
public function login()
{
/** Prepare server response (i.e. state of the model) */
$this->presentationObjectFactory
->build('serverresponse', true)
->setPresentationName('success')
->assignData($this->serviceFactory->build('modelLog')->getModelResponse('success'));
/** Get current visitor information */
$this->presentationObjectFactory
->build('visitor', true)
->assignData($this->serviceFactory->build('recognition')->getCurrentVisitor());
return $this->serviceFactory->build('recognition')->getCurrentVisitor()->isLoggedIn() ?
$this->indexAction() : /* Reroute to index of view */
$this->display('login.html'); /* Show the login template when unsuccesful login*/
}
}
class PresentationObject
{
public function assignData(Collection $visitors)
{
$dateformat = new DateFormat();
$dateTime = new \Datetime();
foreach($visitors as $visitor)
{
$dateTime->setTimestamp($visitor->timestamp);
$this->assign_block_vars('visitor', array(
'ID' => $visitor->id,
'USERNAME' => $visitor->user->Username,
'IP' => $visitor->remote_addr,
'HTTP_USER_AGENT' => $visitor->http_user_agent,
'LAST_SEEN_ONLINE' => ucfirst($dateformat->formatDateDiff($dateTime)),
'DEVICE' => $visitor->getDevice(),
'PLATFORM' => $visitor->getPlatform(),
'BROWSER' => $visitor->getBrowser(),
));
}
}
}
My knowledge and understanding of MVC and domain driven design is strongly influenced by the user tereško, but I have probably misunderstood something on the View part of this explanations...