user1134496
user1134496

Reputation: 53

MVC, am I doing it wrong?

I'm trying to understand MVC thing and so far I know that it's used to separate business logic from the view logic (something as HTML and CSS), but I fail at point when I need to organize my files.

Lets say that I have 3 files:

So looking at my example:

Right?

Upvotes: 5

Views: 333

Answers (6)

vladko
vladko

Reputation: 1033

MVC is rather open to interpretation.

However, in your case it is somewhat clear that "display" is your V (View) and "process" is your M (Model).

The question is... is the "form" really a "C" -- controller?

There are a couple of ways to look at this. Actually, the proper way would be to make your form into service (a pattern used by Symfony, Laravel and ZF2). Then you'd pass the form object down to your view to render the form in display.php and once the form is POST'ed you'd use something like:

// this will likely happen in your controller action 
$form->setData($postData);
$form->validate();

In CakePHP, on the other hand, form is further broken up into validation and user-input "parts". So validation becomes part of your "M" and user-input stays with the "V".

Upvotes: 0

enam
enam

Reputation: 1177

Check out Wikipedia me also started form here to Understand MVC. If you want to develop your own MVC framework you can but I strongly suggest you to first learn the MVC frameworks which already available with Lots of Options(i.e CodeIgniter, Yii etc). Best of luck...

Upvotes: 0

bjudson
bjudson

Reputation: 4083

This isn't really correct, as rao_555 says. The controller would not have a form that is shown to the user, for instance. If you have a form and then a data display, those would both be separate views.

This is a pretty concise description of the design pattern: http://book.cakephp.org/2.0/en/cakephp-overview/understanding-model-view-controller.html

Upvotes: 0

bugs_
bugs_

Reputation: 3744

Not exactly.
process.php is model (It do hard work - working with database)
form.php and display.php is view (It is displayed to user)

But there is no controller. Controller is something like a glue between model and view. Controller get data from View and say: "I will process it by this Model". And after processing it take result from Model and say:"I will display data to user by this View"

Upvotes: 0

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

Wrong, Actually you are mixing Model and Controller in process.php.

form.php and display.php are only interacting with user, they are acting as views.

process.php is acting as both Controller and Model

You should separate the Controller and Model. You can create a separte model.php and do the database stuff there. So if in future you need to change your database stuff. you dont need to touch process.php. Controller and Model will also be separated from each other

Upvotes: 6

warhead
warhead

Reputation: 446

I'd say more like

  • form.php - View
  • process.php - Controller
  • display.php - View

There is no actual model. If you have a data structure to represent the data in someDataClass.php, that would be a model.

What you want is to separate the UI (view), the data processing(controller) and the data definition(model).

Upvotes: 4

Related Questions