Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat purpose does a 1:1 relationship between controller and view serve?
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThe [templates](http://codeangel.org/articles/simple-php-template-engine.html) should be chosen by current view, based on data that you extracted from model. Error-state (which would be triggered by failed command from controller) can be one form of said data. If you encounter error, when opening nonexistence article, that can be handled in two ways: view sending a *HTTP Location* header or view choosing, instead of document displaying template, some template for communicating an error message.
      singulars
    2. CO@tereško What purpose does injecting the view to the controller then serve? Is it simply an architectural decisions for pulling vs. pushing data to the view? I see in your post here: http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000 that you are injecting the View into the controller.
      singulars
    3. COThe current view gets injected in controller because the user's commands are not always targeted at domain model. Take for example: collapsible widgets. If you implement them with PHP as fallback option (in case if JS is not available), then expanding or collapsing the widget would be purely *UI logic*. Controllers can not only change the state of model, but also (in rare cases) the state of view. Which is why controller has a view as a dependency.
      singulars
 

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