Home >Backend Development >PHP Tutorial >PHP development framework Yii Framework tutorial (2) Yii Web application basics
With the previous "Hello, World", Yii application development seems to be easy, don't jump to conclusions so quickly:-), I remember when I first started using MFC development many years ago, after writing the first Hello, World, I still I feel like I don’t know where to start, and I don’t know how to start writing MFC applications. This is because MFC provides a large number of class libraries. If you don’t know about the MFC application framework and the main class libraries it provides in advance, you basically won’t be able to write applications and learn. Yii Framework, you also need to first understand the main components and some commonly used classes that make up Yii applications.
In the previous tutorial, I said that Yii uses MVC (Model-View-Controller), and introduced the entry script and the main application class CWebApplication class. An instance of an application class is created as an object (Singleton) by the entry script. This application singleton object can be accessed from anywhere via Yii::app().
Main application instance (CWebApplication)
By default, the application is an instance of CWebApplication. To customize it, we usually need to provide a configuration file (or array) to initialize its property values when creating the application instance. Another way to customize your application is to extend CWebApplication.
Configuration is an array of key-value pairs. Each key represents the name of a property in the application instance, and each value is the initial value of the corresponding property. For example, the following configuration sets the application's name and defaultController properties.
array( 'name'=>'Yii Framework', 'defaultController'=>'site', )
We usually save these configurations in a separate PHP script (e.g.protected/config/main.php). In the script, we return this configuration array via:
return array(...); To apply this configuration, we pass the name of the configuration file as a parameter to the application's constructor, or like below This is passed to Yii::createWebApplication(). This is usually done in the entry script:
$app=Yii::createWebApplication($configFile);Application component
The functionality of the application can be easily customized or enhanced through its flexible component structure . The application manages a series of application components, each component implements a specific function. For example, the application parses requests from the user with the help of CUrlManager and CHttpRequest.
By configuring the components attribute of the application, we can customize any component class and its attribute values used in the application. For example, we can configure the application's CMemCache component so that it can use multiple memcache servers for caching:
array( ...... 'components'=>array( ...... 'cache'=>array( 'class'=>'CMemCache', 'servers'=>array( array('host'=>'server1', 'port'=>11211, 'weight'=>60), array('host'=>'server2', 'port'=>11211, 'weight'=>40), ), ), ),
) As shown above, we added the cache element to the components array. The cache element indicates that the class of this component is CMemCache, and its servers attribute should be initialized accordingly.
To access an application component, use Yii::app()->ComponentID, where ComponentID refers to the component ID (for example, Yii::app()->cache).
App components can be disabled by setting enabled to false in their configuration. Null will be returned when we access a disabled component.
Life cycle of CWebApplication
When processing user requests, the application will go through the following declaration cycle:
Pre-initialize the application through CApplication::preinit();
Set the autoloader and error handling of classes;
Register core class components;
Load application configuration;
Initialize the application through CApplication::init():
Register application behavior;
Load static application components;
Trigger onBeginRequest event;
Process user requests:
Parse users Request;
Create controller;
Run controller;
Trigger onEndRequest event.
CController Control Class
A controller is an instance of CController or its subclass. It is created by the application when requested by the user. When a controller runs, it performs the requested action, which typically introduces the necessary models and renders the corresponding views. The simplest form of an action is a controller class method whose name starts with action.
Controllers usually have a default action. When the user's request does not specify an action to be performed, the default action will be performed. By default, the default action name is index. It can be modified by setting CController::defaultAction.
The following is the simplest code required for a controller class. Since this controller has no actions defined, requests to it will throw an exception.
class SiteController extends CController { }
Routing
The basic format of the URL for users to access web pages is /index.php?r=XController/YAction, which corresponds to XController.php in the controllers subdirectory of the protected directory with code Define the YAction method of class XController. For example, the route post/edit represents the PostController and its edit action. By default, the URL http://hostname/index.php?r=post/edit requests this controller and action. Model (CModel class) A model is an instance of CModel or one of its subclasses. Models are used to hold data and its related business logic.
Yii implements two types of models: form model and Active Record. Both inherit from the same base class CModel.
The form model is an instance of CFormModel. The form model is used to hold data obtained from the user's input. This data is often acquired, used, and then discarded. For example, in a login page, we can use the form model to represent the username and password information provided by the end user. For more details, please refer to the usage form.
Active Record (AR) is a design pattern for abstracting database access in an object-oriented style. Each AR object is an instance of CActiveRecord or one of its subclasses. Represents a row in the data table. The fields in the row correspond to properties in the AR object.
ViewView
A view is a PHP script that contains the main user interaction elements. He can contain PHP statements, but we recommend that these statements do not change the data model, and it is best to maintain its simplicity (simply as a view). In order to achieve separation of logic and interface, large sections of logic should be placed in the controller or model rather than in the view.
By analogy with Windows applications, View is similar to the Form class of Windows applications, in which controls are used to define the UI. The controls correspond to the Yii framework and become CWidget, which is a component mainly used to represent data. Small objects Usually embedded in a view to produce some complex and independent user interface. For example, a calendar widget can be used to render a complex calendar interface. Widgets make the user interface more reusable.
Masterpage, which is similar to Asp.Net, is called Layout in Yii.
A layout is a special view file used to decorate a view. It usually contains a common part of the view in the user interface. For example: the layout can include the header and footer parts, and then embed the content in between.
......header here......
......footer here......$content stores the content view Rendering results.
When using render(), layout is applied implicitly. The view script protected/views/layouts/main.php is the default layout file. This can be changed by changing CWebApplication::layout or CWebApplication: :layout to customize. To render a view without a layout, call renderPartial() .
The above introduces several important components of the Yii application. You can refer to the "Hello, World" example, and the file structure of the Yii application also has a default definition. The following is the default directory structure of the application:
Before formally developing Yii applications, first take a rough look at the class library provided by Yii http://www.yiiframework.com/doc/api/, which is listed in the figure below Yii Package definition of the class library provided by Framework:
Only by first understanding the main components of Yii applications and the main functions supported by each class package can we use Yii more flexibly in the future. to develop web applications.
The above is the content of the PHP development framework Yii Framework tutorial (2) Yii Web application basics. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!