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

Qiang Xue committed
4 5 6
You now have a working Yii application which can be accessed via URL `http://hostname/index.php`.
In this section, we will introduce what functionalities this application has, how the code is organized,
and how the application handles requests in general.
Qiang Xue committed
7

Qiang Xue committed
8 9 10 11
> Info: For simplicity, throughout this "Getting Started" tutorial we assume that you have set `basic/web`
  as the document root of your Web server. If you have not done so, the URL for accessing
  your application could be `http://hostname/basic/web/index.php`, or something similar.
  Please adjust the URLs accordingly in our descriptions.
Qiang Xue committed
12 13


Qiang Xue committed
14 15
Functionalities
---------------
Qiang Xue committed
16

Qiang Xue committed
17
The application that you have installed has four pages:
Qiang Xue committed
18

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

Qiang Xue committed
25 26
These pages share a common header and footer. The header contains a main menu bar to allow navigate
among different pages.
Qiang Xue committed
27

Qiang Xue committed
28 29 30
You should also see a toolbar sticking at the bottom of the browser window when it displays any of the above pages.
This is a useful [debugger tool](tool-debugger.md) provided by Yii to help you check various debugging information
about the application execution, such as log messages, response status, database queries, and so on.
Qiang Xue committed
31 32


Qiang Xue committed
33 34
Application Structure
---------------------
Qiang Xue committed
35

Qiang Xue committed
36
The following is a list of the most important directories and files in your application,
Qiang Xue committed
37 38

```
Qiang Xue committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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
    runtime/            contains files generated by Yii during runtime, such as logs, cache files
    vendor/             contains the installed Composer packages, including the Yii framework
    views/              contains view files
    web/                application Web root, contains Web accessible files
        assets/         contains published asset files (js, css) by Yii
        index.php       the entry script of the application
    yii                 the Yii console command execution script
Qiang Xue committed
54 55
```

Qiang Xue committed
56 57
In general, the files in the application can be divided into two parts: those under `basic/web` and those
under other directories. The former can be directly accessed from Web, while the latter can/should not.
Qiang Xue committed
58

Qiang Xue committed
59 60 61
Your application uses a single entry `web/index.php`. It is the only PHP script that is directly accessible from Web.
It takes ALL Web requests, creates an [application](structure-applications.md) instance to handle the requests,
and then sends back the responses.
Qiang Xue committed
62

Qiang Xue committed
63 64 65 66
Yii implements the [model-view-controller (MVC)](http://wikipedia.org/wiki/Model-view-controller) design pattern,
as reflected in the above directory organization. The `models` directory contains all [model classes](structure-models.md),
the `views` directory contains all [view scripts](structure-views.md), and the `controllers` directory contains
all [controller classes](structure-controllers.md).
Qiang Xue committed
67

Qiang Xue committed
68
The following diagram shows the static structure of an application:
Qiang Xue committed
69

70
![Static structure of Yii application](images/application-structure.png)
Qiang Xue committed
71 72


Qiang Xue committed
73 74
Request Lifecycle
-----------------
Qiang Xue committed
75 76 77

The following diagram shows a typical workflow of a Yii application  handling a user request:

78
![Typical workflow of a Yii application](images/application-lifecycle.png)
Qiang Xue committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

1. A user makes a request of the URL `http://www.example.com/index.php?r=post/show&id=1`.
   The Web server handles the request by executing the bootstrap script `index.php`.
2. The bootstrap script creates an [[yii\web\Application|Application]] instance and runs it.
3. The Application instance obtains the detailed user request information from an application component named `request`.
4. The application determines which [controller](controller.md) and which action of that controller was requested.
   This is accomplished with the help of an application component named `urlManager`.
   For this example, the controller is `post`, which refers to the `PostController` class, and the action is `show`,
   whose actual meaning is determined by the controller.
5. The application creates an instance of the requested controller to further handle the user's request.
   The controller determines that the action `show` refers to a method named `actionShow` in the controller class.
   The controller then creates and executes any filters associated with this action (e.g. access control or benchmarking).
   The action is then executed, if execution is allowed by the filters (e.g., if the user has permission to execute that action).
6. The action creates a `Post` [model](model.md) instance, using the underlying database table, where the ID value of the corresponding record is `1`.
7. The action renders a [view](view.md) named `show`, providing to the view the `Post` model instance.
8. The view reads the attributes of the `Post` model instance and displays the values of those attributes.
9. The view executes some [widgets](view.md#widgets).
10. The view rendering result--the output from the previous steps--is embedded within a [layout](view.md#layout) to create a complete HTML page.
11. The action completes the view rendering and displays the result to the user.