Yii Fundamentals-Understanding how Yii applications workOn 3 minutes
The way you see Yii framework will fundamentally shape how you understand and work with Yii. Yii implements the model-view-controller pattern which most frameworks have adopted. It is designed from the ground up to be modular. This helps any web application you develop to be separated into separate and interchangeable components. A module is a self-contained software that encapsulates a functionality. This should not be confused with an extension in Yii.
The importance of such an approach as said before, helps you to break down projects into smaller projects you can work on and maintain independently. One can create a user authentication and management module which can be used together with other modules in your Yii web application. Yii module from architectural view is a folder that consists of models, views, controllers and any supporting files.
CModule class provides the bases for the modularity in Yii. It provides the interface that defines the elements that are provided and required by any Yii module.
The properties and methods that are important for our discussion are
string basePath public string getBasePath() public void setBasePath(string $path)
basePath returns the root directory of the module. The importance of this property is that one can place a module anywhere in the file system and set the
basePath property to that location.
array modules public array getModules() public void setModules(array $modules) CModule parentModule -read-only public CModule getParentModule()
A module can contain other modules which can contain other modules as well. The former is the parent module and the later child module. The
modules property of
CModule is used to declare child modules. After initialization of the child modules, their
parentModule property will be set to the parent module.
Here is an example of how to configure sub-modules. Each array element represents a single module, which can be either a string representing the module ID or an ID-configuration pair representing a module with the specified ID and the initial property values. For example, the following array declares two modules:
array( 'admin', // a single module ID 'payment'=>array( // ID-configuration pair 'server'=> 'paymentserver.com',//server property value of payment module ), )
A module is created by calling it constructor. For most part, you will not initialize the module yourself; the framework will do that for you.
/** * Constructor. * @param string $id the ID of this module * @param CModule $parent the parent module (if any) * @param mixed $config the module configuration. It can be either an array or * the path of a PHP file returning the configuration array. */ public void __construct(string $id, CModule $parent, mixed $config=NULL)
The last important property is the behaviour
array $behaviors; the behaviors that should be attached to the module.