PHP Development Framework YII Framework Tutorial (2) YII Web Application Foundation

Source: Internet
Author: User
Tags error handling php script yii

With the front of the "Hello,world", yii application development seems very easy, do not so quickly to conclude:-), think of years ago just began to use MFC development, finished the first Hello,world, or feel the start, do not know how to begin to write MFC applications, This is because MFC provides a large number of class libraries, if you do not understand the MFC application framework and its main class libraries, there is basically no way to write applications, learning the Yii Framework, but also to first understand the composition of yii application of several major components are also some common classes.

In the previous tutorial, Yii used MVC (model-View-controller) and introduced the portal script and the main application class Cwebapplication class. An instance of the application class is created by the portal script as an object (Singleton). This application single Instance object can be accessed anywhere via Yii::app ().

Main application Instance (cwebapplication)

By default, the application is an cwebapplication instance. To customize it, we usually need to provide a configuration file (or array) to initialize the property values when the application instance is created. Another way to customize the application is to inherit cwebapplication.

The configuration is an array of key values. 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 name and Defaultcontroller properties of the application.

Array (
    ' name ' => ' Yii Framework ',
    ' defaultcontroller ' => ' site '
)

We typically save these configurations in a separate PHP script (e.g.protected/config/main.php). In the script, we return this configuration array in the following ways:

Return Array (...);

To apply this configuration, we pass the name of the configuration file as a parameter to the applied constructor, or to Yii::createwebapplication () as follows. This is usually done in the portal script:

$app =yii::createwebapplication ($configFile);

Application components

The functionality of the application can be easily customized or enhanced by its flexible component structure. Applications manage a range of application components, each of which implements a specific function. For example, the application resolves requests from users through Curlmanager and CHttpRequest help.

By configuring the Applied Components property, we can customize any component classes and their property values that are used in the application. For example, we can configure the Cmemcache component of the application so that it can implement caching using multiple Memcache servers:

Array (
    ...
    ') Components ' =>array (
        ...
        ') Cache ' =>array (
            ' class ' => ' Cmemcache ', '
            servers ' =>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 that his servers attribute should be initialized accordingly.

To access an application component, use Yii::app ()->componentid, where ComponentID refers to the ID of the component (for example, Yii::app ()->cache).

The applied component can be disabled by setting enabled to False in its configuration. Null is returned when we access a disabled component.

The life cycle of cwebapplication

When processing a user request, the application undergoes the following declaration cycle:

CApplication::p reinit () pre-initialization application;

Set the automatic loader and error handling of the class;

Registering core class components;

Load application configuration;

Initialize the application by Capplication::init ():

registering the application behavior;

Load static application components;

Triggering onbeginrequest events;

To process user requests:

Parsing user requests;

Create a controller;

Run the controller;

Triggers the Onendrequest event.

Ccontroller Control class

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

The controller usually has a default action. The default action is executed when the user's request does not specify the action to be performed. By default, the default action name is index. It can be modified by setting Ccontroller::d efaultaction.

The following is the simplest code required for a controller class. Because this controller does not have any actions defined, the request to it throws an exception.

Class Sitecontroller extends Ccontroller
{
}

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.