Home >PHP Framework >YII >How to run applications in Yii
After installing Yii, you will have a runnable Yii application. Depending on the configuration, you can use http://hostname/basic/web/index.php or Access http://hostname/index.php. (Recommended study: yii usage tutorial)
This chapter will introduce the built-in functions of the application, how to organize the code, and how the application handles requests in general.
Information: For the sake of simplicity, throughout the "Getting Started" section, it is assumed that you have set basic/web as the root directory of the Web server and configured it. Your address to access the application will be http://hostname/index .php or similar. Please adjust the URL as needed.
Note that the project template and framework are completely different, and they are all yours after installation. You are free to add or remove code and modify everything according to your needs.
Function
An installed basic application contains four pages:
Homepage, when When you visit http://hostname/index.php, the "About" page and "Contact" page are displayed, displaying a contact form that allows end users to contact you via email, "Login" page, displays a login form used to authenticate end users. Try to log in with "admin/admin". You can see that you are currently logged in and you can "log out".
These pages use the same header and trailer. The header contains a navigation bar that allows you to switch between different pages.
You can see a toolbar at the bottom of the browser. This is a very useful debugging tool provided by Yii, which can record and display a large amount of debugging information, such as log information, response status, database queries, etc.
In addition to the web application, there is also a console script called yii, which is located in the application root directory. It can be used for background running and maintenance tasks of programs, as described in the Console Applications chapter.
Application structure
The most important directories and files in the application (assuming the application root directory is basic):
basic/ 应用根目录 composer.json Composer 配置文件, 描述包信息 config/ 包含应用配置及其它配置 console.php 控制台应用配置信息 web.php Web 应用配置信息 commands/ 包含控制台命令类 controllers/ 包含控制器类 models/ 包含模型类 runtime/ 包含 Yii 在运行时生成的文件,例如日志和缓存文件 vendor/ 包含已经安装的 Composer 包,包括 Yii 框架自身 views/ 包含视图文件 web/ Web 应用根目录,包含 Web 入口文件 assets/ 包含 Yii 发布的资源文件(javascript 和 css) index.php 应用入口文件 yii Yii 控制台命令执行脚本
Generally speaking, in the application The files can be divided into two categories: those under basic/web and those in other directories. The former can be accessed directly via HTTP (e.g. a browser), the latter cannot and should not be accessed directly.
Yii implements the Model-View-Controller (MVC) design pattern, which is also reflected in the above directory structure. 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 accessible PHP script in the entire application. The entry script accepts a web request and creates an application instance to handle it. The application parses the request with the help of its components and dispatches the request to the MVC element. Views use widgets to create complex and dynamic user interfaces.
Request life cycle
The following diagram shows how an application handles requests:
The user initiates a request to the entry script web/index.php ask.
The entry script loads the application configuration and creates an application instance to handle the request.
The application resolves the requested route through the request component.
The application creates a controller instance to handle the request.
The controller creates an action instance and executes the filter against the action.
If any filter returns failure, the action is canceled.
If all filters pass, the action will be executed.
The action loads a data model, perhaps from a database.
The action renders a view and provides the data model to it.
The rendering result is returned to the response component.
The response component sends the rendering result to the user's browser.
The above is the detailed content of How to run applications in Yii. For more information, please follow other related articles on the PHP Chinese website!