views:

76

answers:

4

Currently, I am working on restructuring an existing code base. I'm new to php frameworks, but I do know in general how MVC works.

Right now, there is one controller file, one model file, and thirty view files.

Should every model correspond to a table?

Should every view correspond to an html page?

What about the controller? How can I break this thousand line monster into more organized code.

Thanks.

A: 

Every model should correspond to a logical data object - which should generally predominantly be stored in one table (often with foreign keys into other tables, since models generally need to reference other models).

Every view should correspond to a logical way of viewing your data (e.g. on stackoverflow, there is hopefully a view for the list of badges pages, a view for the list of tags pages etc).

Every controller should correspond to a logical grouping of views, which should not be too big (where too big is the line where the file is becoming unmanageable - if you've got 30 views, you can hopefully find a logical way to group them into say 3 controllers).

Dominic Rodger
+1  A: 

Should every model correspond to a table?

No. A model is often constructed from data from multiple sources. Don't think in terms of tying it to your physical database structure even though there will probably end up being lots of similarity.

Should every view correspond to an html page?

Not to sound trite, but every view should correspond to a view. I'm not sure exactly what you mean by a "page".

Perhaps an example would be useful. Imagine a user registration page. The model is User and might contain fields such as:

  • Title
  • Given names
  • Surname
  • Date of Birth
  • Username
  • Address(es)
  • Email address
  • Phone number(s)
  • etc

Now, that data may be in multiple tables. For example: Party, Person, Contact and Address.

There will probably be several views:

  • About page
  • Form page (used for new registration and possibly editing details as well as errors);
  • Success page;
  • Failure page.

Typically all of this will be handled by a single controller as all the processes are inter-related.

cletus
So a thousand line controller is to be expected?
Michael
If you need 1000 lines for a controller to register a user, well, you're doing something wrong.
cletus
A: 

What about the controller? How can I break this thousand line monster into more organized code.

Have a look at CakePHP framework and how it solves the problem of large models, controllers, and numerous views. I find it quite elegant. Complex models can have behaviours. Large controllers can be broken into components. And numerous views are grouped with layouts, while having common bits separated into elements. It might sound complicated and scary at first, but once you try to use it, it really falls into place.

Leonid Mamchenkov
A: 

Should every model correspond to a table?

It doesn't have to but it often will depending on the complexity of your business logic.

Since you're refactoring an existing application, think about how the model is used by the other layers. In MVC, the model is at the bottom of the dependency stack.

How will the view access the model? How will the controller modify it? How will the model be populated?

Should every view correspond to an html page?

Again, it doesn't have to but it often will.

What about the controller? How can I break this thousand line monster into more organized code.

A common strategy is using the front controller pattern. The front controller deals with HTTP requests, application initialisation and site-wide logic (just as your thousand line monster is currently doing) - but then it delegates to more specialised controllers.

These specialised controllers could be grouped by the models it uses, site page structure, or anything else that seems logical. They then interact with the model and select a view to display.

Finally, +1 to frameworks as Leonid suggested. Even if you don't end up using one, there are some great implementations of controller patterns out there.

Hope that helps.

Richard Nguyen