Understanding Yii CApplicationOn Tuesday, 04 September, 2012
One of the basic characteristics of Yii modules is that they cannot be deployed alone. In order to have your web application work, one needs a central module that can process all request, and relay the information to other modules when necessary. This central module should be able to keep application-level configuration settings. This is where the
CApplication becomes useful.
CApplication class extends the
CModule class given a module the ability to be used as a standalone module. It is a module that prescribes how a user request should be handled. It is an abstract class, meaning one cannot create an instance of it. Since it keeps application-level configuration, it defines certain properties to help it achieve its functionality.
charset; the charset currently used for the application. Defaults to 'UTF-8'. controller read-only public CController getController() the currently active controller. Null is returned in this base class. extensionPath public string getExtensionPath() public void setExtensionPath(string $path) Returns the root directory that holds all third-party extensions. homeUrl public string getHomeUrl() public void setHomeUrl(string $value) language public string getLanguage() public void setLanguage(string $language) Returns the language that the user is using and the application should be targeted to. name public string $name; the application name. Defaults to 'My Application'. runtimePath public string getRuntimePath() public void setRuntimePath(string $path) Returns the directory that stores runtime files. sourceLanguage public string $sourceLanguage; the language that the application is written in.
CApplication has six life cycles it undergoes when responding to a user request. The first three is activated when its constructor is called, and the last three is when its
run() method is called.
- load application configuration;
- set up class autoloader and error handling;
- load core application components;
onBeginRequest: An event raised for any attached listener to pre-process the user request;
processRequest:process the user request;
onEndRequest:An event raised for any attached listener to post process the user request;
From the above one can notice one important method
processRequest().It is the place where the actual request processing work is done. It signature is
abstract public void processRequest().
Starting from lifecycle 3, if a PHP error or an uncaught exception occurs, the application will switch to its error handling logic and jump to step 6 afterwards.