O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
• An extension should be self-contained. That is, its external dependency should be<br />
minimal. It would be a headache for its users if an extension requires installation of<br />
additional packages, classes or resource files.<br />
• Files belonging to an extension should be organized under the same directory whose<br />
name is the extension name<br />
• Classes in an extension should be prefixed with some letter(s) to avoid naming<br />
conflict with classes in other extensions.<br />
• An extension should come with detailed installation and API <strong>do</strong>cumentation. This<br />
would reduce the time and effort needed by other developers when they use the<br />
extension.<br />
• An extension should be using an appropriate license. If you want to make your<br />
extension to be used by both open-source and closed-source projects, you may<br />
consider using licenses such as BSD, MIT, etc., but not GPL as it requires its derived<br />
code to be open-source as well.<br />
In the following, we describe how to create a new extension, according to its categorization<br />
as described in overview. These descriptions also apply when you are creating a<br />
component mainly used in your own projects.<br />
Application Component<br />
An application component should implement the interface [IApplicationComponent] or<br />
extend from CApplicationComponent. The main method needed to be implemented is<br />
[IApplicationComponent::init] in which the component performs some initialization work.<br />
This method is invoked after the component is created and the initial property values<br />
(specified in application configuration) are applied.<br />
By default, an application component is created and initialized only when it is accessed for<br />
the first time during request handling. If an application component needs to be created<br />
right after the application instance is created, it should require the user to list its ID in the<br />
CApplication::preload property.<br />
Behavior<br />
To create a behavior, one must implement the [IBehavior] interface. For convenience, <strong>Yii</strong><br />
provides a base class CBehavior that already implements this interface and provides some<br />
additional convenient methods. Child classes mainly need to implement the extra methods<br />
that they intend to make available to the components being attached to.<br />
When developing behaviors for CModel and CActiveRecord, one can also extend<br />
CModelBehavior and CActiveRecordBehavior, respectively. These base classes offer<br />
additional features that are specifically made for CModel and CActiveRecord. For<br />
example, the CActiveRecordBehavior class implements a set of methods to respond to the<br />
life cycle events raised in an ActiveRecord object. A child class can thus override these<br />
methods to put in customized code which will participate in the AR life cycles.<br />
The following code shows an example of an ActiveRecord behavior. When this behavior is<br />
attached to an AR object and when the AR object is being saved by calling save(), it will<br />
automatically sets the create_timeand update_time attributes with the current timestamp.