tp5 框架结构目录

一.安装
1.使用git或者composer(composer update)进行实时更新,区别在于git不会清空核心框架目录而composer会清空。
2.使用官网打包好的TP压缩包(解压即可用)->不是实时更新更新
3.国内的码云和Coding代码托管平台都有ThinkPHP5的镜像


============================================================================================================
二.目录结构
命令行:php think build --module demo
tp5(ROOT_PATH)
├─application(APP_PATH) 应用目录
│ ├─index 模块目录(可更改)
│ │ ├─config.php 模块配置文件
│ │ ├─common.php 模块公共文件
│ │ ├─controller 控制器目录
│ │ ├─model 模型目录
│ │ ├─view 视图目录
│ │ └─validate 验证目录 
│ ├─demo 模块目录(可更改)
│ │ ├─controller 控制器目录
│ │ ├─model 模型目录
│ │ ├─view 视图目录
│ │ ├─config.php 模块配置文件
│ │ └─common.php 模块公共文件 
│ ├─command.php 命令行工具配置文件
│ ├─common.php 应用公共文件
│ ├─config.php 应用配置文件
│ ├─tags.php 应用行为扩展定义文件
│ ├─database.php 数据库配置文件
│ └─route.php 路由配置文件
├─extend 扩展类库目录(可定义)
├─public 网站对外访问目录
│ ├─index.php 应用入口文件
│ └─static 静态资源目录 
│ ├─css 样式目录
│ ├─js 脚本目录
│ └─img 图像目录
├─runtime 运行时目录(可定义)
├─vendor 第三方类库目录(Composer)
├─thinkphp(THINK_PATH) 框架系统目录
│ ├─lang 语言包目录
│ ├─library 框架核心类库目录
│ │ ├─think think 类库包目录
│ │ └─traits 系统 traits 目录
│ ├─tpl 系统模板目录
│ │
│ ├─.htaccess 用于 apache 的重写
│ ├─.travis.yml CI 定义文件
│ ├─base.php 框架基础文件
│ ├─composer.json composer 定义文件
│ ├─console.php 控制台入口文件
│ ├─convention.php 惯例配置文件
│ ├─helper.php 助手函数文件(可选)
│ ├─LICENSE.txt 授权说明文件
│ ├─phpunit.xml 单元测试配置文件
│ ├─README.md README 文件
│ └─start.php 框架引导文件
├─build.php 自动生成定义文件(参考)
├─composer.json Composer定义文件
├─LICENSE.txt 授权说明文件
├─README.md README 文件
└─think 命令行工具入口

目录和文件
目录使用小写+下划线;
类库、函数文件统一以 .php 为后缀;
类的文件名均以命名空间定义,并且命名空间的路径和类库文件所在路径一致;
类文件采用驼峰法命名(首字母大写),其它文件采用小写+下划线命名;
类名和类文件名保持一致,统一采用驼峰法命名(首字母大写)

=========================================================================================================================
三.控制器
1.URL不区分大小写。
2.控制器类可包括多个操作方法,但是protected、private类型无法直接通过URL访问(public 类型才可通过URL访问)

四.视图
1.存放模板文件(hello.php)。
2.Index控制器类继承了thinkController类之后,我们可以直接使用封装好的assign和fetch方法进行模板变量赋值和渲染输出。

fetch方法中我们没有指定任何模板,所以按照系统默认的规则(视图目录/控制器/操作方法)输出了view/index/hello.html模板文件。

总结:
use thinkController;=>class Index extends hinkController


===========================================================================================================================
URL:
1.入口文件可以隐藏。(public/index.php)

2.方法名自动转化成小写。

3.方法中进行传参,通过url中参数值进行模板中返回。
完整路由:http://tp5.com/入口文件/模块名/控制器名/方法名/参数名/参数值
完整路由:http://tp5.com/index.php/index/index/hello/res/thinkphp
*如果 public function hello($res=' World')中进行了指定默认值,参数可进行忽略。
定义name(参数可选):http://tp5.com/index.php/index/inedx/hello
*修改配置文件url_param_type=1按照顺序获取可多参数传递。http://localhost/tp5/public/index.php?s=index/index_test/hello/res/a
public function hello($res='',$data=''){
if(!empty($res)){
return 'Hello,'.$res.'!'.$data;
}else{
return 'Hello, World !!!';

}
输出结果:Hello,res!a

4.命名:驼峰式命名HelloWorld=>hello_world(如果支持严格区分大小写并支持驼峰法进行控制器访问修改配置文件url_convert)。
*修改配置文件url_convert后严格要求控制器类名。
*pathifo方式:http://localhost/tp5/public/index.php/index/index_test/hello/res
*兼容方式 :http://localhost/tp5/public/index.php?s=index/index_test/hello/res


路由简化:
*路由简化在我的理解上一个是节省了url的长度,方便记忆和查看;一个是因为使用自定义路由,能防止恶意请求,对于网站的安全有帮助。
route.php:'hello'=> 'index/IndexTest/hello',
use thinkRoute;
Route::rule('hello','index/IndexTest/hello');-->http://localhost/tp5/public/hello/a/b
http://localhost/tp5/public/res/a/b
输出结果:Hello,a!b
*使$res变成可选参数:'hello/[:res]'=> 'index/IndexTest/hello',
*使用route起别名时程序限制性return中的数据。
亦可使用闭包定义路由
return[
'hello'=> function($res='',$data=''){
if(!empty($res) || !empty($data)){
return 'Hello,'.$res.'!!! ------'.$data;
}else{
return 'Hello, World !!!';

},
];
Route::rule('hello',function($res='',$data=''){
if(!empty($res) || !empty($data)){
return 'Hello,'.$res.'!!! ------'.$data;
}else{
return 'Hello, World !!!';

});
*可使用别名进行url的规则约束,比如定义请求方式,定义后缀名。


===================================================================================================
五。数据库操作
原生:
$result = Db::execute('insert into think_data (id, name ,status) values (5, "thinkphp",1)');
$result = Db::query('select * from think_data where id = 5');
(原则上,读操作都使用query方法,写操作使用execute方法即可)
$result = Db::connect('db1')->query('select * from think_data where id = 1');
链式:
$list = Db::name('data')
->where('status', 1)
->field('id,name')
->order('id', 'desc')
->limit(10)
->select();
*对于事务的支持,最简单的方法就是使用transaction方法,只需要把需要执行的事务操作封装到闭包里面即可自动完成事务


====================================================================================================
六、数据显示
1.foreach标签
foreach标签用于循环输出:
foreach(name,item,key)
name(必须):要输出的数据模板变量
item(必须):循环单原变量
key(可选):循环的key变量,默认值为key
示例:
<foreach name='list' item='vo'>
{$vo.id}
{$vo.name}
</foreach>
foreach标签相对于volist标签简洁,没有volist标签那么多功能。优势是可以对对象进行遍历输出,而volist标签通常是用于输出数组。
--------------------------------------------------------------------------------------------------------------------------------
2.volist标签
volist标签主要用于在模板中循环输出数据集或者多维数组
volist(name,id,offset,length,key,mod,empty)
name(必须):要输出的数据模型变量
id(必须):循环变量
offset(可选):要输出数据的offset
length(可选):输出数据的长度
key(可选):循环的key变量,默认值为i
mod(可选):对key值取模,默认为2(用于偶数行查询等)
empty(可选):如果数据为空显示的字符串
通常模型的select方法返回的结果是一个二维数组,可以直接使用volist标签进行输出。
----------------------------------------------------------------------------------
举例:
在模板定义如下,输出编号和姓名
<volist name='list' id='vo'>
{$vo.id}
{$vo.name}
</volist>
支持输出部分数据,例如输出其中的第5-15条记录:
<volist name='list' id='vo' offset='5' length='10'>
{$vo.id}
{$vo.name}
</volist>
输出偶数行记录:
<volist name='list' id='vo' mod='2'>
<eq name="mod" value='1'>{$vo.name}</eq>
</volist>
mod属性还用于控制一定记录的换行:
<volist name='list' id='vo' mod='5'>
{$vo.name}
<eq name="mod" value='4'><br/></eq>
</volist>
输出循环变量:
<volist name='list' id='vo' key='k'>
{$k}.{$vo.name}
</volist>
如果没有指定key属性的话,默认使用循环变量i,例如:
<volist name="list" id="vo" >
{$i}.{$vo.name}
</volist>
如果要输出数组的索引,可以直接使用key变量,和循环变量不同的是,这个key是由数据本身决定,而不是循环控制的,例如:
<volist name="list" id="vo" >
{$key}.{$vo.name}
</volist>
volist还有一个别名iterate,用法和volist是一样。
从2.1版开始允许使用函数设定数据集,如:
<volist name=":fun('arg')" id="vo">{$vo.name}</volist>

原文地址:https://www.cnblogs.com/my2018/p/10626772.html