Code style

You are viewing revision #3 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#2)next (#4) »

  1. Where to put what
  2. Widgets

Where to put what

Many time, writing widgets or editing Gii code, there can be the doubt if the code is following the MVC patter or not.

A few suggestion can help to detected the most evident error of style.

Controller

In the controller there should be the code for collect user input, retrive models from database, rendering views.

In a controller there should never be:

  • html code (it should be in the view)
  • sql code (there should never be, if is needed it should stay in a model)
  • field name they should be in the view.

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

Views

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

In the view there should not be:

  • user input ($_GET, $_POST) input should be collected in models in the controllers, never in views.
  • sql code: if is neede, better to crete functions in the models

The less php operation there are in the views, the best it is. Instead of concatnate 2 fields, is better to write a getter method, so you can reuse in other views.

Models

The models are used for collect user inputs and access to the database.

In the models there should not be:

  • 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 has a behavior similar to controllers. If a widget is supposed to create lot of html, better to create a view file for it.

29 0
7 followers
Viewed: 28 504 times
Version: Unknown (update)
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

Related Articles