Reading the doc

  1. Reading the Yii doc
  2. $this: the second good friend in Yii

Reading the Yii doc

What to read:

Almost all the information you need whilst working with yii is written in the Class Reference.

The point is just to know how to find what you need.

To find this information, you have to understand what is Yii::app() and what is "this" in the context in wich you are.

Yii::app(): your best friend during programming

In each point of your application code (view, controller, widget, model, module) you can call Yii:app().

This will give you the actual instance of CApplication that is running.

If you are developing a Web application, you will get an instance of CWebApplication, if you are developing a Console app, you will get an instance of CConsoleApplication.

This object is the one that is configured through config/main.php, is the backbone of Yii. So in main.php you can configure each property you see, or add new compomentes that can be invoked via Yii::app()->myComponent.

What can I do with this Yii::app()?

You can do whatever is written here: CApplication.

Taking a look at this page, you will notice lot if interesting stuff, like db, urlManager, numberFormatter and many other.

So, if you need to format a number, you can use the actual number formatter, calling Yii::app()->numberFormatter->

And then what? Simply click on the link named numberFormatter on the doc page, and you will be redirected to the CNumberFormatter page, where you can see all possible properties, methods and event available.

$this: the second good friend in Yii

Another important element is the variable $this. It is the actual object we are implementing. So in a controller, it will be the actual controller, you can check all possible methods on this page.

Remember that when you are in a view of a controller (so not in protected/components/views but in protected/views/controllerID), $this is an instance if CController too, the one that called render or renderPartial.

If you are in a model, $this is the instance of CModel (CFormModel or CActiveRecord).

21 0
3 followers
Viewed: 12 795 times
Version: 1.1
Category: Tips
Tags: doc
Written by: zaccaria
Last updated by: Orteko
Created on: Oct 18, 2010
Last updated: 14 years ago
Update Article

Revisions

View all history

Related Articles