Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat purpose does a 1:1 relationship between controller and view serve?
    text
    copied!<p>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.</p> <p>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: <a href="https://stackoverflow.com/a/18983927/1681418">https://stackoverflow.com/a/18983927/1681418</a></p> <pre class="lang-php prettyprint-override"><code>class View { public function render($templateFile, array $vars = array()) { ob_start(); extract($vars); require($templateFile); return ob_get_clean(); } } </code></pre> <p>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 <em>do-stuff-to-the-model</em> section (=controller) and a <em>read-only-from-model</em> section (=view). I have however a few shortcomings that I have yet to find a tidy solution for, namely:</p> <ul> <li>Where should the template file be selected?</li> <li>How does the view get to know about errors in the model?</li> <li>How does the view get to know about successful or unsuccessful command/action in controller?</li> <li>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.</li> </ul> <p>I have no trouble rendering correct output on my page, but my current approach feels <em>wrong</em>. **Anyone got an example of a view that uses domain driven design with the model as a layer, not a class? **</p> <p><a href="https://stackoverflow.com/a/18983927/1681418">This answer</a> is very similar to what I usually find, and I do not understand how this approach uses or requires a 1:1 relationship.</p> <p>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.</p> <pre class="lang-php prettyprint-override"><code>class Controller { public function login() { $this-&gt;serviceFactory -&gt;build('recognition') -&gt;authenticate($this-&gt;request-&gt;username, $this-&gt;request-&gt;password); } } class View { public function login() { /** Prepare server response (i.e. state of the model) */ $this-&gt;presentationObjectFactory -&gt;build('serverresponse', true) -&gt;setPresentationName('success') -&gt;assignData($this-&gt;serviceFactory-&gt;build('modelLog')-&gt;getModelResponse('success')); /** Get current visitor information */ $this-&gt;presentationObjectFactory -&gt;build('visitor', true) -&gt;assignData($this-&gt;serviceFactory-&gt;build('recognition')-&gt;getCurrentVisitor()); return $this-&gt;serviceFactory-&gt;build('recognition')-&gt;getCurrentVisitor()-&gt;isLoggedIn() ? $this-&gt;indexAction() : /* Reroute to index of view */ $this-&gt;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-&gt;setTimestamp($visitor-&gt;timestamp); $this-&gt;assign_block_vars('visitor', array( 'ID' =&gt; $visitor-&gt;id, 'USERNAME' =&gt; $visitor-&gt;user-&gt;Username, 'IP' =&gt; $visitor-&gt;remote_addr, 'HTTP_USER_AGENT' =&gt; $visitor-&gt;http_user_agent, 'LAST_SEEN_ONLINE' =&gt; ucfirst($dateformat-&gt;formatDateDiff($dateTime)), 'DEVICE' =&gt; $visitor-&gt;getDevice(), 'PLATFORM' =&gt; $visitor-&gt;getPlatform(), 'BROWSER' =&gt; $visitor-&gt;getBrowser(), )); } } } </code></pre> <p>My knowledge and understanding of MVC and domain driven design is strongly influenced by the user <a href="https://stackoverflow.com/users/727208/teresko">tereško</a>, but I have probably misunderstood something on the View part of this explanations... </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload