Zend Framework - Do we really need to use Models?
I have been programming in PHP for almost 10 years now and recently just got into MVC. I tried a few MVC frameworks like CakePHP, CodeIgniter, and Zend Framework. I even programmed my own. I know most of us do not want to program our own MVC frameworks.
Do you really need to use models in MVC programming?
Now going through Zend Framework, I realized that we really do not really need models as the code used in models can easily be used in controllers. Now before the MVC experts say anything, please read on.
My question and answer is really pointed at other MVC beginners and I built a few MVC sites with Zend Framework with just controllers and views. The controllers even worked when I called certain libraries like Zend_Db into them.
So, for those learning MVC, if you find this programming concept overwhelming, concentrate on controllers and views first. Now, the bottom line is that for very simple websites, we do not need models, especially at the time when traditional PHP coders are learning MVC.
What about large scale websites?
Ok, NOW models may be needed. Being the business logic of the entire MVC concept, most of the programming should go here. When you see that your controllers are becoming very inundated with PHP code, it probably is the time to start using models.
Controllers are nothing but bridges between views and models.
From what I understand, controllers are supposed to be very slim, meaning that they should contain very little code. Your models can and should have most of the code. The controllers are simply a bridge that will tell the models to perform all of the processing and then whatever data the models produce will be ported over to your views as regular variables. If your controllers contain too much code, they will start looking like spaghetti code, which is one of the issues that MVC is supposed to avoid. If that happens, move your controller codes into a model.
Putting this all together for MVC beginners:
For small scale websites: If you are just learning MVC, stick with the views and controllers and learn them. For best practice, try to use a model if possible.
For large websites, start implementing your models and if you have code in your controllers that entails any type of processing, move them into the models and point their data to your views.
More by this Author
I just recently discovered OpenCart as a very powerful, but easy-to-use PHP based open source ecommerce platform. OpenCart is written in PHP's MVC framework. With OpenCart, you can add products with attributes, options,...
I have been a PHP programmer for over 10 years and have been programming in the straight top-down method since the days of Visual Basic, but I have found how popular MVC (Model View Controller) frameworks are,...
Drupal is the most difficult to use out of all the content management systems, but a lot of major corporations are using it. It also has the longest learning curve for both web designers and web developers. Drupal's...