1

In the ideal world one should not rely on singletons, the model in the controller and model inside the view would be 2 different instances. The problem arises when the controller sets a state and the view presentation depends on that state. For example:

  class MyController extends Controller {

    public function __construct(ModelUsers $cModel)
    {
      $this->model = $cModel;
    }

    public function action_Search($username) {
      $this->model->filterByUsername($username);
    }
  }

  class MyView extends View {

    public function __construct(ModelUsers $vModel)
    {
      $this->model = $vModel;
    }

    public function users() {
      return $this->model->getUsers();
    }
  }

How to share data between the controller model and the view model?

user1517081
  • 965
  • 2
  • 11
  • 30
  • Why would a singleton be bad ? – Virus721 Jul 17 '13 at 07:30
  • It's not a "a model". Model is a layer and layers cannot be Singletons. Likewise, there is not a controller model or a view model. See http://stackoverflow.com/questions/3499336/in-mvc-where-do-you-draw-the-line-between-a-controller-and-model/3501048#3501048 for more details. – Gordon Jul 17 '13 at 07:33
  • 2
    @Virus721 because http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323 – Gordon Jul 17 '13 at 07:34
  • @Virus721 there are a lot of reasons why Singleton is called antipattern. They hide lifecycle of object, they hide dependiences. They control their lifecyle which violates SRP. There are really a lot of reasons. – Robert Jul 17 '13 at 07:34
  • Who cares about lifecycle in PHP ? We're not coding a 3D graphical engine, just a short procedure that generates HTML. I don't see what's the problem about not controlling the life cycle of the singleton. And you can still create a deleteInstance method anyway. – Virus721 Jul 17 '13 at 07:35
  • @Virus721 if you are just coding short procedures that generate HTML and you don't care about lifecycles anyway, you have even less need for a Singleton. So why bother? – Gordon Jul 17 '13 at 07:43
  • In MVC2, there's only one controller, makes sense to make a singleton, same thing for the database. So yeah why would someone try to create a second instance ? Very unlikely, but it still looks cleaner to make it impossible. – Virus721 Jul 17 '13 at 07:47
  • 2
    @Virus721 It doesn't make sense at all. Your code should have exactly what it needs to do it's work. Nothing more. Nothing less. Singleton is for **enforcing** singularity *and* providing **global access** and your examples don't need that. So if it's not a constraint, don't add it. See my answer linked above and all the links in that. – Gordon Jul 17 '13 at 07:50

2 Answers2

2

Starting from basics

A view requests from the model the information that it needs to generate an output representation to the user.

It means the view should be only responsible for showing the information. Just for that. You can also do some things like triming, chaning text size etc. but you shouldn't do some countings there or more complicated operations.

A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands.

Model should be responsible for doing data operations. You can use it for example to get the records from database. It just be responsible for data handling.

A controller can send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document). It can also send commands to the model to update the model's state (e.g., editing a document).

Controler is kind a proxy between model and view. You get there params and according to this params you set proper action of your controller. This action should create correct model object and use it to get data then assign to the view.

I've never used singleton in models. If you need some classes that would help MVC structure you can use helpers and as Hast suggested Registry pattern. I'm not a fan of using singleton.

You may also want to look at When to use singleton

So your question.

  • Controler -> model = Passing data via arguments of model's methods
  • Model -> controler = If reference then just work on it, if argument then do something and return result
  • Controler -> view = assign proper data to be viewed.
  • View->controller = go to special url to make data or use ajax request to retrieve it.
Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
0

You can use Registry or Dependency injection instead.

Also in some cases you may pass some data to your view class as array. Like this:

  class MyView extends View {
    private $data = array();

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function users() {
      return $this->data['model']->getUsers();
    }
  }

Of course you have to pass model when you caling the View class from your controller (or wherever you make call).

ozahorulia
  • 9,798
  • 8
  • 48
  • 72