Where to put what ¶
Often, when writing widgets or editing Yii code, it is unclear whether or not the code is following the MVC pattern.
Here are a few suggestions to help detect the most obvious style errors.
Controller ¶
The controller should contain code for collecting user input, retrieving models from the database, and rendering views.
In a controller there should never be:
- HTML code: HTML should be in the view
- SQL code: If needed, SQL should be in a model, encapsulate in methods.
- Field names: Field names should be in the view
Avoiding embedding field names allows you to change the database easily.
Views ¶
HTML code should only be in views.
Views should not contain:
- User input (e.g. $_GET and $_POST): Input should be collected in models in the controllers, never in views.
- SQL: When needed, it is best to create a function in the model
The fewer PHP operations there are in the views, the better it is. Instead of concatenating two fields, it is better to write a getter method, so you can reuse it in other views.
Models ¶
Models 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
Widgets ¶
Widgets have a behaviour similar to controllers. If a widget is supposed to create a lot of HTML, it is better to create a view file for it.
re junxiong
This part of field name is the most weak of the article.
What I mean, is that the field name (property of model) shold appear in the model, for example in the specification of rules, attribute label, search and so on.
Should appear in the view, in $form->textField($model, 'fieldName').
About the controller, I think that is better to avoid to hardcode the field names.
The default Gii code creates the function search() and setAttributes, in order to delegate to the model the deals with the actual database structure.
If you have to set some default value, I think that the doing in the controller is correct too.
For example, if you save 2 models in relation belongs to, you will save the main model first, and on the related you will do something like:
$model->foreignKey_field=$model->primary_key;
In this case, you are writing in the controller the name of the foreign key field, and I think that this is an absolutely stylish exception to my rules.
If you want to be more strict, as Yii provide an alias for primary key, you can add a function in the related model like:
public function setMainModelId($id)
{
$this->foreignKey_field=$id;
}
This will alias the field name for the foreign key field, and will allow you to change the database implementation without touching the controller.
As the change of the name of a foreign key field is an even more than rare in real life, I 100% quote the first option.
Definitive guide
This article has been reviewed, expanded and included in the definitive guide by samdark.
Read also this chapther of the guide.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.