Difference between #2 and #5 of
Code style

Changes

Title unchanged

Code style

Category unchanged

Tips

Yii version unchanged

Tags unchanged

mvc

Content changed

Where to put what ------------------ Many time,Often, when writing widgets or editing GYii code, there can be the doubt ifit is unclear whether or not the code is following the MVC patter or not.
 
 
A
n.
 
 
Here are a
few suggestion cans to help to detected the most evident error of styleobvious style errors. ### Controller In tThe controller there should be thecontain code for collecting user input, retriveving models from the database, and rendering views. In a controller there should **never** be:
 
- htmlHTML code (it: HTML should be in the view) - sqlSQL code (there: If needed, SQL should never be, if is needed it should stay in a model)n a model, encapsulate in methods. - fField name theys: Field names should be in the view. Avoiding embedding field names allows you to change the database with minimal efforteasily. ### Views In the views (and only in the views) there should be the html code. 
 
 
In the view there
HTML code should only be in views.
 
 
Views
should **not be:** contain:
 
- uUser input (e.g. $_GET, and $_POST) i: Input should be collected in models in the controllers, never in views. - sql code: if isSQL: When needed, betterit is best to create functions in the models The less phpfewer PHP operations there are in the views, the bestter it is. Instead of concatenate 2ing  two fields, it is better to write a getter method, so you can reuse it in other views. ### Models The m
 
M
odels are used for collecting user inputs, and access toing the database. In the models there should **not** be:
 
- uUser input ($_GET, $_POST)y: You should write a function that will be called in the controller. - html code (itHTML: HTML should be in thea view)
 
Widgets ------------------ Widgets hasve a behaviour similar to controllers. If a widget is supposed to create lot of html,HTML, it is better to create a view file for it.
 
29 0
7 followers
Viewed: 28 570 times
Version: 1.1
Category: Tips
Tags: mvc
Written by: zaccaria
Last updated by: g3ck0
Created on: Nov 9, 2010
Last updated: 14 years ago
Update Article

Revisions

View all history