0
votes

I have previously asked this question but didn't get an answer. I'm trying to learn model view controller pattern and developing a simple framework for learning purposes.

I understand it is hard to adopt mvc pattern to php and most of the popular frameworks use mvc variations (including myself), not the actual pattern itself. In most of these variations: controller gets input from the user and sends it to the model, model process this input and gives a response back to the controller and in final step, controller chooses the appropriate view and sends information to this view based on model's response.

In my design, controllers use a view instance that simply look like this:

class view {
    public function render($filename){
        require_once "templates/$filename.php";
    }
    public function addData($key,$value){
        $this->{$key}=$data;
    }
}

In this context, what is the best way for the controller to send regularly displayed information to the view?

To give an example, let's say we have a social network application which has a top menu that displays notifications to the user. This menu is, core part of the view and being displayed on every page of the application. To my understanding, this means that once the user is logged in to the application, controllers must add notification information to the view instance in every action method.

I was wondering if there is a more elegant way to do this.

1

1 Answers

1
votes

Your suspicion of a more elegant way is correct, and "pure" MVC is not the best solution for complex apps like this. Angular.JS and other frameworks that use a single-page-app style design address this issue from one perspective. Another answer to these issues are HMVC or MVVC design patterns. A combination of these patterns is used, depending on the needs of the application.

To more directly answer your question within that MVC mindset, you should use a controller that inherits from something like a LoggedInUserController. This controller would always load models like notificaitons. The views likewise would be extensions of a LoggedInUserView that always has a placeholder for the notifications and knows how to consume that notification model.

class LoggedInUserController extends MVCController {   
  protected function init() {
    $this->_top_menu_model = new TopUserMenuModel('getAllMenuModels');
    $this->_footer_model = new UserFooterModel('getAllMenuModels');
    //any other essential widget models
  }
}

class UserPage1 extends LoggedInUserController {
  function init() {
    parent::init();
    //other code here
  }
}

There are many other approaches, and the functionality, size, and budget of your application should drive the design.

You want to look at HMVC frameworks for a direct answer to the way you're going now, with the "I need widgets" problem. http://en.wikipedia.org/wiki/Hierarchical_model%E2%80%93view%E2%80%93controller