小总结以及补充

thinkphp5需要PHP的版本大于5.4,mysql的版本也要新一点。

通常开发网站的时候,都是这么创建文件夹的。某个模块里面创建对应MVC架构的controller和model和view文件夹

这几个thinkphp5的配置文件,作用如下:
command.php 如果用命令行控制thinkphp5,那么就会读取这个配置文件中的内容
common.php 如果想写一些所有模块都能使用的通用函数,那么就写在这个文件中
config.php 这个是thinkphp5的配置文件,整个应用都会读取这个配置文件
database.php 连接数据库的时候,作数据库配置,就修改这个文件
route.php 美化url(缩短url长度)的时候,修改这个文件
tags.php 框架扩展文件

继续解释这几个文件夹中文件的作用:
extend 是第三方库下载放的文件夹,只要修改相应的命名空间就可以使用
public 网站根目录,所有的访问都要先访问public文件夹下的index.php文件
favicon.ico 访问网站的时候现实的网页标签
robots.txt 给搜索引擎看的文件,告诉搜索引擎哪些页面可以爬,哪些不可以爬。通常只能爬取前端页面,后台登录页面之类的不给爬
static 资源文件,下载的模板的资源都放在这里
router.php 框架快速启动文件。若没有装apache,那么可以利用这个文件快速启动WebServer

继续解释各个文件的作用:
runtime 网站缓存文件,包括缓存日志,编译文件等
thinkphp 框架文件,框架的内容全在这个文件夹
base.php 定义一些框架中的常量
composer.json 定义composer的一些配置
console.php 控制台的入口文件
convention.php 框架默认的配置文件
helper.php 这是入口函数
lang 这个文件夹里面是源包
library 这个是框架的核心,里面有两个目录,一个think,一个traits
LICENSE.txt 这个是说明文件
logo.png 这个是thinkphp的logo
phpunit.xml 这个是phpunit的测试文件
README.md 这个是说明文件
start.php 是框架启动文件
tpl 里面是框架默认的一些模板

library里面两个文件:
think 是整个框架的核心文件
traits 是类库的扩展文件

default_index.tpl 这个是自动生成的控制器模板文件
dispatch_jump.tpl 这个是网站发出成功或者失败中间的跳转文件
page_trace.tpl 这个是我们调试时显示的模板文件
think_exception.tpl 这个是页面抛出异常时显示的模板文件

最后一波文件夹解释:
vender 是利用composer安装thinkphp5生成的文件,通过conposer安装的所有类库都会安装到这个文件夹中






实例:
开发项目的时候,文件创建的条理性就是这么一回事

如果创建了common模块,那么thinkphp5框架就规定从外部无法访问这个模块,这个是规范。这个模块有其他的作用。

如果想要让namespace命名空间的app看起来更直观一点,只需要做两件事,第一件事就是修改application文件夹名字为app,第二件事,进入public文件夹,修改index.php这个入口文件,将里面的路径修改成app而不是application

在不同的开发环境中,php的配置很重要。针对不同的环境,进行不同的配置。
有两种进行配置的方法,一种是使用配置函数,另一种是在配置文件中进行配置。
先看使用配置函数的方法:


对于配置函数config()里面若是不传入任何的形参,那么就会返回当前的全部配置。也就是说,里面可以传形参来进行配置。
第二种方法就是直接到config.php配置文件中进行手动修改。手动修改当前配置又或者自己添加新的配置。。。
下面这个就是创建conf文件夹的原因:

换句话说,这么做的好处就是,我们在开发中可以清晰的知道自己对配置做了哪些修改。更何况,在这里做的修改,都会对app中的模块起作用。(前提是在入口文件那定义了路径'CONF_PATH')
这个文件夹必须叫做extra

另外,thinkphp5会默认的把email.php这个文件的名字作为一个数组,当修改email.php为emails.php的时候,看下图:

学上面这种情况是为了讲解下面的database.php的设置
通常情况下,如果项目比较大,配置较多,会在extra这个扩展配置中再增加数据库的配置。

但是,也有一个特例,那就是实际的项目开发中,或许根本用不到那么多的配置,也就是说database.php可以不放在extra文件夹中,直接放在和extra同级的一层。虽然放在同级的一层,可是database.php的优先级依旧比config.php高

场景配置:
thinkphp5支持在不同的场景中使用不同的配置文件,这一点我个人感觉比较溜。在家中就可以使用家中的配置,在公司就可以使用公司的配置。
第一步,增加场景配置

第二步,构造相应的场景

只不过这里需要注意的是,目前thinkphp5可能还存在这个bug,那就是在home.php或者office.php中,必须要返回数据库的全部配置,否则,只能显示你在home.php或者office.php中进行的配置(也许也会显示配置失效的效果)

第三步,进行场景切换
只需要修改config.php中的 'app_statue'=>'值' 值为office或者home就可以自由的切换配置了。

模块配置:
上面学的内容是对所有模块都起作用的配置。下面的内容是针对某个模块进行的配置。
若想针对某个模块进行配置,那么就必须在conf中创建相应的和那个模块同名的文件夹,比如index,然后,在到index这个创建的文件夹中创建config.php文件,最后在这个config.php文件中进行配置即可。

这样的配置都是生效的

动态配置:主要是对当前控制器或某个方法进行动态改变
若想配置某个控制器,那么可以利用魔术方法__construct(),魔术方法只对当前这个控制器管用。若想对某个方法进行配置,那么就只要在这个方法中使用config()函数来配置即可,不会对其他方法起作用,只对当前方法起作用。

Config类和config方法:
若要使用config类,那么必须先引入文件,也就是use think\Config,使用的时候,直接像使用静态方法那样使用。

实际上,上面的 $res=Config::get()就等于$res=config() 这两者效果是一样的。
若想要详细了解config()函数的功能编写,可以在helper.php中进行查看

然而实际开发中,最好使用Config类的方法。原因是如果有人事先定义了config()函数,那么,我们使用config()函数的时候,可能就是别人定义过的config函数。那么效果就不一样了。

上面的config('?username')是检验配置中是否有这个键值。。。
如果想深入了解Config.php类中的方法作用,可以直接看thinkphp/library/think/Config.php文件
如果想看config()函数是如何编写的,去thinkphp/helper.php中查看