Mazzy
Mazzy

Reputation: 14189

MVC pattern: what needs to be created first?

I'm studying CodeIgniter and its software patterns. Which should be created first, the view or the controller?

Upvotes: 9

Views: 4851

Answers (8)

Baimyrza Shamyr
Baimyrza Shamyr

Reputation: 549

I know this might be old question ,but I bumped into it now.
And someone else might need it later. So I decided to summarise it.
I think that your approach will depend on situation.
Let the order of operations you make be this Model-View-Controller
And lets call it Bottom-To-Top approach. The case and reasoning for this is provided by @Gordon here .
And another case lets call it Top-To-Bottom, when you start with UI. The case and reasoning for this is provided by @shiplu.mokadd.im here.
Evaluate your own case and choose one of them. Good luck!

Upvotes: 1

user895378
user895378

Reputation:

Though the question already has an accepted answer I'd like to offer an approach from a testability standpoint. Ready-made frameworks often don't design their controllers with testability in mind, so this sort of question arises over and over.

A well-written, testable controller should take the view as a dependency injected via the controller's __construct method. I don't know if CodeIgniter controllers allow for this functionality, though.

In the MVC paradigm the Controller "brings together" the elements of the View and the Model, so, like any behavioral dependency, if these objects provide any functionality beyond "dumb" data storage, they should be injected into your controller at the time of instantiation so that they can be mocked for testing purposes. So you would do something like the following:

$view  = new SmartyView;
$model = new UserModel;
$controller = new LoginController($view, $model);

Often, the model doesn't provide any public "behavior" methods and acts as a basic data storage entity. If this is the case, for the same reason you don't need to inject an array data structure into your controller to create new arrays, you can safely create new model instances inside your controller without sacrificing testability:

class LoginController
{
  protected $view;

  public function __construct(ViewInterface $view)
  {
    $this->view = $view;
  }
  public function doLogin($user, $pass)
  {
    $userModel = new UserModel();
    // do stuff with model to determine if user/pass was valid
  }
}

$view  = new SmartyView;
$controller = new LoginController($view);

For testability, the best practice is to avoid tightly coupling your controllers to your views and models. This is why you should generally avoid using the new keyword inside your controller code. Your controller -- and any other object, for that matter -- should ASK for its dependencies, not LOOK for them. This will result in more transparent APIs, more testable code, less debugging hassle and a happier you.

Upvotes: 1

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

I always start with View. The scenario is, I have already designed my database, have chosen technologies. This is my workflow on MVC pattern.

  1. Create html template with CSS/image etc.
  2. Depending on what data is shown on view I create Controller methods.
  3. I put dummy data in controller methods so that I can see a dynamic page that manipulates the view properly.
  4. According to the data my controller needs I creates Model method.

In this process I never create any extra method or code block. It prevents to add code that we usually think "might be necessary". But they are never needed.

In this way you are creating specification first then implementing it on each step. So its like, View creates data requirements for it. Controller provides it. And creates data specification to Model. At last Model just provide only those data which are called or needed.

Upvotes: 8

itachi
itachi

Reputation: 6393

I would say model and controller hand in hand.

Without model, how will you know what will be the flow in the controller?

Without controller, how will you know what methods are needed in model?

Sometimes, model gets priority, sometimes controller depending upon the situation.

Upvotes: 0

Prasad Rajapaksha
Prasad Rajapaksha

Reputation: 6190

Controller is must. Because you can't execute/get the work done without having a controller. So controller comes first because of following,

  1. You can disable the view when necessary
  2. You can set relevant variables to the view via controller
  3. Interaction with the model is doing by the controller, so when you require data the controller comes first
  4. You can redirect the user to other controller
  5. You can display other view based on the request. For computers view A, and for mobile devices view B, like wise
  6. View is the presentation, so first you need data to present.

Upvotes: 1

Gordon
Gordon

Reputation: 316999

The Model because that is your application. Controller and View only form one interface to the Model. One could say, the controller is the door to your house. What do you build first? The door or the house? Right, so build the Model first. Then add an interface to it.

Upvotes: 15

bumbu
bumbu

Reputation: 1317

You should start with Controller as it is calling View.

Here you can see that Controller is intermediate between Model and View. http://codeigniter.com/user_guide/overview/mvc.html

Upvotes: 1

Lyuben Todorov
Lyuben Todorov

Reputation: 14163

The controller, you need it to communicate between the model and the view. Without the controller the model cant interact with the view.

Upvotes: 0

Related Questions