Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller vs. Model - Need explanation

I'm on the beginning of my "Learn MVC" way. Basically, I don't have big problems with object-oriented programming however there's one technical aspect that needs clarification. It seems that my theory isn't quite good enough.

Currently, I'm using KohanaPHP framework, version 3.

Example situation: I have a website, where user can submit an article.

So I have following structure:

classes/
    /controllers/
        article.php
    /models/
        articles.php

So far so good. I don't have problems with models that extends Kohana_Model however I'm not sure if I'm using the correct way models that are using ORM.

Basically when using models extending Kohana_Model I'll put all logical operations in model. Should I do the same for models using ORM? In many example over the Net I saw controllers that was performing logical operations on user input/data from database which is incorrect in my opinion.

Let's say I need to get few rows from database so I create proper method in model and returning the object. I think it's correct, isn't it?

Basically, all operations on user input/data (select from db, insert into db, validation) I put in the models. That's the way I understand MVC design pattern. Models should take care about all "mechanic" operations and controller is only a "bridge" between models/views and it's a "front" engine.

Is it a correct approach?

I know that might be a silly question for more advanced users however I want to learn only good practices. If anyone could deliver some clarification, I'll be delighted.

like image 210
M. A. Avatar asked Jan 11 '11 10:01

M. A.


People also ask

What do you put in a model VS controller?

The main advice is simple. If the code is “doing something”, put it in the controller. If the code is “knowing something”, put it in the model. Another example that can be used to understand the difference between the model and controller is thinking about how to code a chess application.

Do you need a controller for every model?

Do I need a controller for each model? No, not necessarily. However, having one controller per RESTful resource is a convention for a reason, and you should carefully analyze why that convention isn't meeting your needs before doing something completely different.

Why do we need controller in MVC?

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

Should models know about controllers?

The model should contain the business logic and the controller enforces it --- this is an important distinction to make. The controller shouldn't know why something happens, but it should know how to respond when stuff happens.


2 Answers

In short terms, your model performs all operations on the data (be it incoming, outgoing, database, files ... data), and your view should take care of displaying the data. The controller should call the necessary model methods to get the data ready to be passed to the view. The controller shouldn't perform any changes to the data, but it should test it to get the necessary action done properly.

Hope I made it clear enough, let me know if this doesn't clear things for you.

like image 167
Poelinca Dorin Avatar answered Oct 21 '22 05:10

Poelinca Dorin


I haven't worked with KohanaPHP but it looks like a 'rails-inspired' Framework. In the rails world it is generally considered best practice to have skinny controllers and fat models.

some background can be found in this old article by jamis buck http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model or in this one concerning cakephp http://gluei.com/blog/view/cakephp-best-practices-fat-models-and-skinny-controllers

like image 31
roman Avatar answered Oct 21 '22 04:10

roman