HMVC pattern - how do all the triads M-V-C share the same view template/css/etc.?

StackOverflow https://stackoverflow.com/questions/17138390

  •  31-05-2022
  •  | 
  •  

Domanda

How do all the different modules (or packages or however you call them) composed of models-controllers-views share the same site design (template, includes like header and footer, assets like css, js, etc.) in a HMVC architecture as all the triads have their own View folder ?

È stato utile?

Soluzione

You really don't want modules to share the same views. Typically what you want is a common view, the outer view. In HMVC the controller is the middle man between the view and the model. All requests will be routed to a controller action. Your initial request could be to a controller that instantiates a view (outer view), then within that same controller you make a request to a module controller which gives a view as a response. The initial controller takes the module controllers view response and sets it to a section of the outer view.

Class Controller_Page extends Controller {
/**
*Initial controller
*/
public function action_index() {
  //get the outer controller view
  $outerView = View::factory('outermost');
  //request to a module called widget
  $widgetView = Request::factory('widget');
  //add the widget to the body of the outer view
  $outerView->body = $widgetView;
  $this->response->body($outerView);
  }
}

Class Controller_Widget extents Controller {
/**
*Module Controller
*/
public function action_index() {
   $view = View::factory('widget');
   //set the widget view as the response
   $this->response->body($view);
   }
}

//contents of "outermost" view
<html>
<body>
<!--The $body property was binded to the view by the contoller ("$outerView->body") -->
<!-- Binding of properties to views is how data is passed to the view -->
<?php echo $body; ?>
</body>
</html>

//contents of "widget" view
<p>
I'm a widget
</p>

//the final result will be
<html>
<body>
<p>I'm a widget</p>
</body>
</html>

Also note that in HMVC there is a Model, View, and Controller but it's not the "MVC architecture" you hear talked about. It more follows the Presentation Abstraction Control (PAC) architecture. In the MVC architecture the model notifies the view of changes, this is not the case with PAC.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top