MVC or just pretending?

I used to think I hated MVC. I realized in the past few months that MVC is great, but not many programmers actually adhere to it in the PHP world.

A lot of PHP frameworks pay lip service to MVC without actually adhering to any strict standards. I see this resulting in (1) duplicated code, (2) duplicated information, and (3) a messy hodge-podge of PHP+HTML, almost every day.

The first symptom (duplicated code) happens because of sloppy programming more than not sticking to MVC. However, the problem is much worse because functional code is placed in the controller, the model, AND the view. Functional code should be placed only in the controller (or “helpers” or “libraries” called from the controller, as the case may be depending on your framework).

The second symptom (duplicated information) occurs because information is placed in the controller and the view. It should only be in the model. For example, form validation: the controller should validate form data, but all validation information should be in the model.

The third symptom occurs because functional code and information is placed in the view. Only the layout and UI design should go into the view. Form validation, sending emails, saving to the database – that’s outside the boundaries of the view, or it should be.

I’m still trying to flesh all these details out. One thing I know for sure that will improve a project by leaps and bounds – don’t allow PHP code in the view! At all. Ever. Some programming philosophies state that, because PHP is a templating language itself, HTML and PHP should be mixed together. I think that’s wrong. It leads to messy, hard to manage, and even dangerous code. My solution is the template engine in PHP-Frame.

This entry was posted in Coding, News and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>