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.
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 behttp://hostname/basic/web/index.php
, or something similar. Please adjust the URLs accordingly in our descriptions.
The application that you have installed contains four pages:
- 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".
These pages share a common header and footer. The header contains a main menu bar to allow navigate among different pages.
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 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.
The following is a list of the most important directories and files in your application,
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
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 not and should not.
Yii implements the model-view-controller (MVC) design pattern
which is reflected in the above directory organization. The models
directory contains all model classes,
the views
directory contains all view scripts, and the controllers
directory contains
all controller classes.
The following diagram shows the static structure of an application.
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 instance to handle it.
The application resolves the request with the help of its components
and dispatches the request to MVC. Widgets are used in the views
to help build complex and dynamic user interface elements.
The following diagram shows how an application handles a request.
- A user makes a request to the entry script
web/index.php
. - The entry script loads the application configuration and creates an application instance to handle the request.
- The application resolves the requested route with the help of the request application component.
- The application creates a controller instance to handle the request.
- The controller creates an action instance and performs the filters for the action.
- If any filter fails, the action is cancelled.
- If all filters pass, the action is being executed.
- The action loads a data model, possibly from a database.
- The action renders a view with the data model.
- The rendering result is to the response application component.
- The response component sends the rendering result to the user.