start-workflow.md 5.3 KB
Newer Older
Qiang Xue committed
1 2 3
Running Applications
====================

Carsten Brandt committed
4 5 6 7
After installing Yii, you have a working Yii application that can be accessed via
the URL `http://hostname/basic/web/index.php` or `http://hostname/index.php`, depending
upon your configuration. This section will introduce the application's built-in functionality,
how the code is organized, and how the application handles requests in general.
Qiang Xue committed
8

Larry Ullman committed
9
> Info: For simplicity, throughout this "Getting Started" tutorial, it's assumed that you have set `basic/web`
10
  as the document root of your Web server, and configured the URL for accessing
Larry Ullman committed
11 12
  your application to be `http://hostname/index.php` or something similar.
  For your needs, please adjust the URLs in our descriptions accordingly.
Qiang Xue committed
13 14


15
Functionality <span id="functionality"></span>
Carsten Brandt committed
16
-------------
Qiang Xue committed
17

Larry Ullman committed
18
The basic application installed contains four pages:
Qiang Xue committed
19

Carsten Brandt committed
20 21 22 23
* The homepage, displayed when you access the URL `http://hostname/index.php`,
* the "About" page,
* the "Contact" page, which displays a contact form that allows end users to contact you via email,
* and the "Login" page, which displays a login form that can be used to authenticate end users. Try logging in
Qiang Xue committed
24
  with "admin/admin", and you will find the "Login" main menu item will change to "Logout".
Qiang Xue committed
25

Larry Ullman committed
26
These pages share a common header and footer. The header contains a main menu bar to allow navigation
Qiang Xue committed
27
among different pages.
Qiang Xue committed
28

Larry Ullman committed
29 30
You should also see a toolbar at the bottom of the browser window.
This is a useful [debugger tool](tool-debugger.md) provided by Yii to record and display a lot of debugging information, such as log messages, response statuses, the database queries run, and so on.
Qiang Xue committed
31 32


33
Application Structure <span id="application-structure"></span>
Qiang Xue committed
34
---------------------
Qiang Xue committed
35

Larry Ullman committed
36
The most important directories and files in your application are (assuming the application's root directory is `basic`):
Qiang Xue committed
37 38

```
Qiang Xue committed
39 40 41 42 43 44 45 46
basic/                  application base path
    composer.json       used by Composer, describes package information
    config/             contains application and other configurations
        console.php     the console application configuration
        web.php         the Web application configuration
    commands/           contains console command classes
    controllers/        contains controller classes
    models/             contains model classes
Larry Ullman committed
47 48
    runtime/            contains files generated by Yii during runtime, such as logs and cache files
    vendor/             contains the installed Composer packages, including the Yii framework itself
Qiang Xue committed
49 50
    views/              contains view files
    web/                application Web root, contains Web accessible files
Carsten Brandt committed
51
        assets/         contains published asset files (javascript and css) by Yii
Larry Ullman committed
52
        index.php       the entry (or bootstrap) script for the application
Qiang Xue committed
53
    yii                 the Yii console command execution script
Qiang Xue committed
54 55
```

Larry Ullman committed
56
In general, the files in the application can be divided into two types: those under `basic/web` and those
Carsten Brandt committed
57
under other directories. The former can be directly accessed via HTTP (i.e., in a browser), while the latter can not and should not be.
Qiang Xue committed
58

Larry Ullman committed
59
Yii implements the [model-view-controller (MVC)](http://wikipedia.org/wiki/Model-view-controller) design pattern,
60
which is reflected in the above directory organization. The `models` directory contains all [model classes](structure-models.md),
Qiang Xue committed
61 62
the `views` directory contains all [view scripts](structure-views.md), and the `controllers` directory contains
all [controller classes](structure-controllers.md).
Qiang Xue committed
63

64 65 66
The following diagram shows the static structure of an application.

![Static Structure of Application](images/application-structure.png)
Qiang Xue committed
67

68 69
Each application has an entry script `web/index.php` which is the only Web accessible PHP script in the application.
The entry script takes an incoming request and creates an [application](structure-applications.md) instance to handle it.
Larry Ullman committed
70 71
The [application](structure-applications.md) resolves the request with the help of its [components](concept-components.md),
and dispatches the request to the MVC elements. [Widgets](structure-widgets.md) are used in the [views](structure-views.md)
72
to help build complex and dynamic user interface elements.
Qiang Xue committed
73 74


75
Request Lifecycle <span id="request-lifecycle"></span>
Qiang Xue committed
76
-----------------
Qiang Xue committed
77

78 79
The following diagram shows how an application handles a request.

80
![Request Lifecycle](images/request-lifecycle.png)
81 82 83 84 85 86 87 88 89

1. A user makes a request to the [entry script](structure-entry-scripts.md) `web/index.php`.
2. The entry script loads the application [configuration](concept-configurations.md) and creates
   an [application](structure-applications.md) instance to handle the request.
3. The application resolves the requested [route](runtime-routing.md) with the help of
   the [request](runtime-requests.md) application component.
4. The application creates a [controller](structure-controllers.md) instance to handle the request.
5. The controller creates an [action](structure-controllers.md) instance and performs the filters for the action.
6. If any filter fails, the action is cancelled.
Larry Ullman committed
90
7. If all filters pass, the action is executed.
91
8. The action loads a data model, possibly from a database.
Larry Ullman committed
92 93 94
9. The action renders a view, providing it with the data model.
10. The rendered result is returned to the [response](runtime-responses.md) application component.
11. The response component sends the rendered result to the user's browser.
Qiang Xue committed
95