安装YII
php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.13
安装好了之后,可以启动Apache服务器,并且访问YII框架:
http://localhost/DemoPage/Yii/basic/web/index.php
这是我安装好的目录文件:注意(最下面有个bat脚本,这个脚本的作用可以参考页面:
http://www.yiiframework.com/doc-2.0/guide-tutorial-console.html)
每个目录的作用,请看下面:
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 and cache files
vendor/ contains the installed Composer packages, including the Yii framework itself
views/ contains view files
web/ application Web root, contains Web accessible files
assets/ contains published asset files (javascript and css) by Yii
index.php the entry (or bootstrap) script for the application
yii the Yii console command execution script
其中需要注意的是,所有的目录其实可以分为两个部分,一个是HTTP协议可以访问的目录,为web 目录, 其它的目录都不应该可以被http直接访问。
同时,web目录中有个启动文件,也就是index.php文件。
页面如下:(注意,该页面带有YII框架自带的一个DEBUG TOOL,使用方式可以参考:
https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md)
下面介绍一下YII框架的架构方面的内容:
-
YII处理请求的流程:
一个请求在框架中的生命周期如下:
下面是对请求生命周期的详细描述:
- 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 executed.
- The action loads a data model, possibly from a database.
- The action renders a view, providing it with the data model.
- The rendered result is returned to the response application component.
-
The response component sends the rendered result to the user's browser.