控制器和视图之间的1:1关系服务的目的是什么?
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...
What purpose does a 1:1 relationship between controller and view serve?
There is no need for a strict 1:1 rule if the view can be applied in multiple workflow contexts / controllers / models. Keeping a view and controller separate, even if they are only used together and can be merged, is a best practice for a clear separation of responsibilities. It also makes it easier to swap and share views later.
Where should the template file be selected?
In your case you have a View class, so it should be defined there. In some systems the template is the view and is often selected in the controller or configuration files.
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?
In some architectures the models return their issues to the controller during updates or any other actions. In the controller I usually add any problems to a user message stack. Then in the view I output those messages to the user.
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.
This I've seen handled in quite a few different ways. The most robust solution is for your controller to pass the next workflow steps to the view. The view should basically stay as agnostic as possible to any business logic.
Any example code would be too long and elaborate for SO. I would start with good MVC framework tutorials: