Difference between #3 and #4 of
Code style

Revision #4 has been created by auxbuss on Nov 9, 2010, 1:41:36 PM with the memo:

tidied and tightened up the English for clarity
« previous (#3) next (#5) »


Title unchanged

Code style

Category unchanged


Yii version unchanged

Tags unchanged


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.
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 should never be, if is: If needed it, SQL should staybe in a model) - 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.
should not becontain: - 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
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
Viewed: 25 031 times
Version: 1.1
Category: Tips
Tags: mvc
Written by: zaccaria
Last updated by: g3ck0
Created on: Nov 9, 2010
Last updated: 11 years ago
Update Article


View all history