5

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:

  • form.php which is displayed to the user, it takes user input and submits data
  • process.php which takes and handle data from form.php, then connects to database and retrieve requested informations
  • display.php which display processed data (result) from process.php in organized way

So looking at my example:

  • form.php would be controller
  • process.php would be model and
  • display.php would be view

Right?

  • 2
    Take a look at source code of any of the prominent PHP frameworks: CI/Kohana, Cake, etc ... this should give you a better idea of how MVC can be implemented in a PHP project –  Jan 06 '12 at 15:06

6 Answers6

6

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

Rajesh Pantula
  • 10,061
  • 9
  • 43
  • 52
  • So in process.php I shall write code that will handle user input (validate it for example), then that processed data process.php sends to database.php which interacts with database and retrieve requested informations. In this case I'll have 2 views (form.php, display.php), 1 controller (process.php) and 1 model (database.php) – user1134496 Jan 06 '12 at 16:04
  • exactly.. ya you will have 2 views. – Rajesh Pantula Jan 06 '12 at 16:11
4

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).

warhead
  • 446
  • 2
  • 3
  • Also, he mixes `v` and `c` if there is anything displayed on `display.php`. `form.php` displays information to the user (view), manipulates/routes the user's data (controller), and represents application data in the db (model) –  Jan 06 '12 at 14:59
0

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".

vladko
  • 1,013
  • 14
  • 21
0

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"

bugs_
  • 3,544
  • 4
  • 34
  • 39
0

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

bjudson
  • 4,073
  • 3
  • 29
  • 46
0

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...

enam
  • 1,179
  • 1
  • 10
  • 24