PHP development framework YiiFramework tutorial (2) YiiWeb application basics

Source: Internet
Author: User
Only by understanding the main components of the Yii application and the main functions supported by each class package can Yii be used to develop Web applications flexibly in the future.

With the previous "Hello, World", Yii application development seems very easy. don't draw a conclusion so quickly: -) I think that when I first started using MFC development many years ago, I wrote the first Hello, after World, I still don't know how to write the MFC application. this is because MFC provides a large number of class libraries, if you do not know the MFC application programming Framework and the main class libraries it provides in advance, you will not be able to write applications and learn the Yii Framework, you also need to know the main components of the Yii application and some common classes.

In the previous tutorial, Yii adopts MVC (Model-View-Controller) and introduces the entry script and the main application class CWebApplication class. An instance of the application class is created by the entry script as an object (Singleton ). This application Singleton object can be accessed anywhere through Yii: app.

Master application instance (CWebApplication)

By default, an application is a CWebApplication instance. To customize it, we usually need to provide a configuration file (or array) to initialize its attribute value when creating an application instance. Another way to customize an application is to inherit CWebApplication.

The configuration is an array of key-value pairs. Each key represents the name of an attribute in the application instance. each value is the initial value of the corresponding attribute. For example, the following configuration sets the application name and defaultController attributes.

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, the configuration array is returned in the following way:

Return array (...); to apply this configuration, we pass the name of the configuration file as a parameter to the application constructor, or as follows to Yii: createWebApplication (). This is usually done in the portal script:

$ App = Yii: createWebApplication ($ configFile); application component

The functions of an application can be easily customized or enhanced through its flexible component structure. Applications manage a series of application components, each of which implements a specific function. For example, the application parses user requests through the help of CUrlManager and CHttpRequest.

By configuring the components attribute of an application, we can customize any component classes and their attribute values used in the application. For example, you can configure the application's CMemCache component so that it can use multiple memcache servers to implement cache:

Array (
......
'Components' => array (
......
'Cache' => array (
'Class' => 'cmcache ',
'Servers' => array (
Array ('host' => 'server1', 'port' => 11211, 'weight' => 60 ),
Array ('host' => 'server2', 'port' => 11211, 'weight' => 40 ),
),
),
),
As shown above, the cache element is added 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 ).

The application component can be disabled by setting enabled to false in its configuration. When we access a disabled group, Null is returned.

CWebApplication lifecycle

When processing user requests, the application goes through the following declaration cycle:

Preinitialize the application through CApplication: preinit;

Set the automatic loader and error handling of the class;

Register core components;

Load application configuration;

Use CApplication: init () to initialize the application:

Register application behavior;

Load static application components;

Triggers the onBeginRequest event;

Process user requests:

Parse user requests;

Create a controller;

Operation controller;

Triggers the onEndRequest event.

CController control

The controller is an instance of CController or its subclass. It is created by the application when the user requests it. When a controller is running, it executes the requested action. The action usually introduces the necessary model and renders the corresponding view. The simplest form of an action is a controller class method whose name starts with action.

The controller usually has a default action. When the user's request does not specify the action to be executed, the default action will be executed. By default, the default action is index. It can be modified by setting CController: defaultAction.

The following is the simplest code required by a controller class. Because this controller does not define any action, an exception is thrown for its request.

Class SiteController extends CController
{
}

Routing

The basic URL format for a user to access a webpage is/index. php? R = XController/YAction, corresponding to the YAction method of the class XController defined in XController. php under the controllers subdirectory under the code protected directory. For example, post/edit indicates PostController and its edit action. By default, URL http: // hostname/index. php? R = post/edit: this controller and action are requested. A model (CModel class) is an instance of a CModel or its subclass. The model is used to maintain data and related business logic.

Yii implements two types of model: Form model and Active Record. Both of them inherit from the same base class CModel.

The form model is an instance of CFormModel. The form model is used to preserve data obtained from user input. The data is often obtained, used, and discarded. For example, on a logon page, we can use a form model to indicate the user name and password provided by the end user. For more details, see use forms.

Active Record (AR) is a design pattern used to abstract database access through the image-oriented style. Each AR object is an instance of CActiveRecord or its subclass. Represents a row in the data table. The fields in the row correspond to the attributes in the AR object.

View

A view is a PHP script that contains the main user interaction elements. it can contain PHP statements, but we recommend that these statements do not change the data model, and it is best to keep it simple (simply as a view ). To separate the logic from the interface, the logic of the large segment should be placed in the controller or model, rather than in the view.

Similar to a Windows application, View is similar to the Form class of a Windows application. you can define the UI using a control component, and the control corresponds to the Yii Framework as a CWidget, it is a component mainly used to represent data. small objects are usually embedded in a view to generate complex and independent user interfaces. for example, a small calendar object can be used to render a complex calendar interface. small objects make the user interface more reusable.

Masterpage, similar to Asp. Net, is called Layout in Yii ).

A layout is a special view file used to modify a view. it usually contains some common views on the user interface. for example, the layout can contain the header and footer parts, and then embed the content in the layout.

... Header here ......

...... Footer here ...... $ content stores the rendering results of the content view.

When render () is used, the layout is implicitly applied. view script protected/views/layouts/main. php is the default layout file. this can be customized by changing CWebApplication: layout or CWebApplication: layout. To render a view without layout, call renderPartial ().

The preceding section describes several important components used by Yii. you can refer to the "Hello, World" example, and the file structure of the Yii application is also defined by default, the following is the default directory structure of the application:

 

Before formally developing the Yii application, let's take a rough look at the class library http://www.yiiframework.com/doc/api/ provided by Yii, listing the package definition of the class library provided by Yii Framework:

Only by understanding the main components of the Yii application and the main functions supported by each class package can Yii be used to develop Web applications flexibly in the future.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.