Difference between #4 and #3 of Code style

Code style
Where to put what

Many time,Often, when writing widgets or editing
GiiYii code, there can be the doubt ifit
is unclear whether or not the code is following the MVC patter or

AHere are a few suggestion can
helpsuggestions to detectedhelp detect
the most evident error of style.obvious style errors.

### Controller

In theThe controller there should be
thecontain code for collectcollecting
user input, retriveretrieving models from the
database, and rendering views.

In a controller there should never be:

 - html code (itHTML code: HTML should be in the
 - sql code (thereSQL code: If needed, SQL should
never be, if is needed it should staybe in a
 - field name theyField names: Field names should be in
the view.view

Avoiding embedding field names allowallows you to change
the database with minimal effort.easily.

### Views

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

In the view thereViews should not

- user input ($_GET, $_POST)User input (e.g. $_GET
and $_POST): Input should be collected in models in the controllers, never
in views.
- sql code: ifSQL: When needed, it is neede,
betterbest to crete functionscreate a
function in the modelsmodel

The less php operationfewer PHP operations there are in
the views, the bestbetter it is. Instead of
concatnate 2concatenating  two fields, it is
better to write a getter method, so you can reuse it in other views.

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

InModels are used for collecting user inputs, and
accessing the database.

In models there should not be:

- User input ($_GET, $_POST): You should write a function that will be called in
the controller. 
- HTML: HTML should be in a view

- 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 hashave a behaviorbehaviour
similar to controllers. If a widget is supposed to create a lot of
html,HTML, it is 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,544 times
  • Created on: Nov 9, 2010
  • Last updated: Nov 9, 2010
  • Tags: mvc