Difference between #3 and #2 of Code style

Code style
Where to put what

Many time, writing widgets or editing Gii code, there can be the doubt if the
code is following the MVC patter or not.

A few suggestion can help to detected the most evident error of style.

### Controller

In the controller there should be the code for collect user input, retrive
models from database, rendering views.

In a controller there should never be:

 - html code (it should be in the view)
 - sql code (there should never be, if is needed it should stay in a model)
 - field name they should be in the view.

Avoiding embedding field names allow you to change the database with minimal

### Views

In the views (and only in the views) there should be the html code. 

In the view there should not be:

- user input ($_GET, $_POST) input should be collected in models in the
controllers, never in views.
- sql code: if is neede, better to crete functions in the models

The less php operation there are in the views, the best it is. Instead of
concatnate 2 fields, is better to write a getter method, so you can reuse in
other views.

### Models
The models are used for collect user inputs and access to the database.

In the models there should not be:

- user input ($_GET, $_POST)you should write a function that will be called in
the controller. 
- html code (it should be in the view)


Widgets has a behavior similar to controllers. If a widget is supposed to create
lot of html, better to create a view file for it.
Write new article
  • Written by: zaccaria
  • Updated by: g3ck0
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +30 / -1
  • Viewed: 18,752 times
  • Created on: Nov 9, 2010
  • Last updated: Nov 9, 2010
  • Tags: mvc