Difference between #4 and #3 of Code style

unchanged
Title
Code style
unchanged
Category
Tips
unchanged
Tags
mvc
changed
Content
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
not.pattern.

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
view)view
 - sql code (thereSQL code: If needed, SQL should
never be, if is needed it should staybe in a
model)model
 - 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
be:contain:

- 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
database.

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
------------------

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: +29
  • Viewed: 13,455 times
  • Created on: Nov 9, 2010
  • Last updated: Nov 9, 2010
  • Tags: mvc