下载 官网:下载了一个完整版的ThinkPHP5以及一个完全开发手册 配置 解压压缩包到根目录并且改名(方便以后操作) 目录结构 开发手册中有描述大概就是 application ->应用目录 extend->拓展库目录 public ->对外访问目录 runtime->运行时目录 vendor->插件目录 thinkphp->框架系统目录 入口文件 入口文件是 public 文件夹下面的index.php文件 这个文件会自动运行 application->index->controller->index.php文件的index方法 开启debug模式 application 文件夹下面有一个配置文件 config.php 把app_debug=>'false'false改为true即可 url传值规则 访问http://www.myweb.com/tp5/public/index.php这个url时会自动访问到application index controller index.php这个文件中的index方法 是thinkphp默认的 http://www.myweb.com/tp5/public/index.php/index/index/index这个路径也可以访问到index,php中的index方法 前面的http:www.myweb.com/tp5/public/index.php是唯一入口文件 必须访问 后面的index/index/index分别代表模块/控制器/方法/ 我们现在在application index(模块) controller index.php (控制器) 中新建了一个test方法
public function test($name,$id){ return $name.$id; }我们可以通过这条URL : http://www.myweb.com/tp5/public/index.php/index/index/test/name/jim/id/007 不仅访问到了test方法 还给方法传了值 index/index/test/模块 控制器 方法 name/jim/id007参数名/值/参数名/值 参数的传递不分先后 快速入门数据库 增删改查基本都有四种方法 即 原生语句 查询构造器方法 去前缀方法 以及利用db助手的方法 开始之前在本机数据库bbs中新建一个表think_data 建表语句如下:
create table think_data( -> id int not null primary key auto_increment, -> name varchar(255) not null, -> status tinyint(2) not null default 0 -> )engine = myisam default charset=utf8;application 下面有一个数据库配置文件database.php 在里面填写修改相关的信息 比如账号 密码 数据库名 前缀等信息 当需要使用到数据库的时候框架就会拿到这些信息自动连接 . 四种插入数据的方式
/*public function add($name,$status){ #原生方法 $sql = "insert into think_data(name,status) value('{$name}',{$status})"; $result = Db::execute($sql); dump($result);//返回受影响行数 int(1) #构造器方法必须传入完整的数据库名 $result = Db::table('think_data') ->insert(['name'=>"{$name}",'status'=>"{$status}"]); dump($result);//INT(1) Db::table('表名') ->insert(['列名'=>值,'列名'=>值]); 库中的表统一了表前缀,在application/database.php中的也写明prefix => '前缀_' 就可以使用 在建表的时候已经人为加上think_ 并且也在databases.php中告诉了系统 think_是前缀 $result=Db::name('data') ->insert(['name'=>"{$name}",'status'=>"{$status}"]); dump($result);//INT(1) #db助手 $db =db('data'); $result = $db->insert(['name'=>"{$name}",'status'=>"{$status}"]); dump($result);//(int1) #db 助手返回自增长id $result = db('data')->insertGetId(['name'=>"{$name}",'status'=>"{$status}"]); dump($result);//string(1) "5" 每一个[]符号中存放一行数据 }*/原生方式 : sql db::execute(sql); 构造器: Db::table(全名)->insert([数据]); 去前缀 Db::name(去前缀)->insert([数据]); db助手 db(去前缀) ->insert/insetyGetId([数据]); 使用数据库操作之前需要用到Db类 use think\Db; 4中修改方式
public function update($id,$name,$status){ #原生方法 $sql = "update think_data set name='{$name}',status='{$status}' where id ={$id}"; $result = Db::execute($sql); dump($result);//INT(1) 返回受影响行数*/ //构造器方法 $result=Db::table('think_data')->where('id',"=","{$id}")->update(['name'=>"{$name}",'status'=>"{$status}"]); dump($result);//int(1) ///去前缀name方法 $result = Db::name('data')->where('id',"=","{$id}")->update(['name'=>"{$name}",'status'=>"{$status}"]); dump($result);//int(1) //db助手 $db =db('data'); $result=$db->where('id',"{$id}")->update(['name'=>"{$name}",'status'=>"{$status}"]); dump($result);//int(1) //->where('列',"比较符",'具体值'), }查询方式依旧有四种 和上面基本相同 表对象->过滤条件->select(); 下面以db对象简单举个例子
public function select(){ $db = db('data'); $result = $db->where('id',"<","3")->limit(2)->select(); dump($result); }返回的是一个多维数组 第一层是下标0123…n代表第几行数据 第二层是个关联数组 列名为索引 删除数据也是上面四种方法 这里以第四种db助手为例
public function delete ($id){ $db = db('data'); $db->delete($id); //删除多个 $db->delete([1,2,3,]); }模版入门VC 1使用think空间下的Controller类use \think\Controller 2继承Controller类 extende Controller 以上两步可化成一步 extends \think\Controller 然后在方法中调用$this->fetch()方法 系统就会自动寻找调用模版 比如 index类下面的index方法 系统会自动去调用 aplication index view index index.html 如果是test 方法中的fetch会自动去调用 aplication index view index test.html 总之就是到view视图文件夹下找到控制器名字的文件夹 然后进去找方法名字的模版文件
use \think\Controller; class Index extends Controller { public function index(){ return $this->fetch(); //aplication/index/view/index/index.html } public function test(){ return $this->fetch(); //aplication/index/view/index/test.html } }模版与数据库结合 主要是利用smarty模版技术 将查询到的数据分配给smarty系统调用assign()方法 然后在模版中利用smarty模版技术将数据渲染出来
public function index(){ $db=db('data'); $result=$db->where('id','<','4')->select(); #smarty模板技术 $this->assign('result',$result); return $this->fetch(); } <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <!--接收到了一个查询到的二维数组--> {$result.0.name} </body> </html>smarty提供了两种遍历数组的方式
<!--接收到了一个查询到的二维数组$result--> {foreach $result as $key=>$value} {foreach $value as $k=>$v}{$k}:{$v}<br/> {/foreach} {/foreach} {volist name="result" id="vo" offset='1' length='2' empty="暂时无数据"} {$i}->{$vo.id} {$vo.name} {$vo.status}<br/> {/volist} <!--volist专门针对查询出来的二维数组 name数组值 id指的是每一行数据的对象 后面省去了二次遍历 直接拿id.索引即可 --> <!--offset的意思是从第几行数据开始遍历 不包括他本身 length是遍历几个数据--> <!--{$i}默认存在的值 是二维素组的第一层数组的值 也就是代表第几行数据 总之volist针对的是第二维度的数组 --> <volist name=值来源 id="代表着遍历出的整行数据对象" offset=从第几行开始遍历 不包括本身 length=长度 empty=没有值时显示的数据留言板展示页面 aplication index controller 下面重新建一个bbs,php控制器 并且加入控制器的命名空间```namespace app\index\controller; 这样的话就可以通过入口文件访问到Bbs.php 并且会自动寻找Bbs类下面的index方法访问 然后继承控制器 这样就可以通过调用fetch方法 来调用模版了 模版放在 application index view bbs 文件夹下面 文件名是方法名 做Bbs之前专门为留言板在bbs 数据库中新建了一个表think_bbs
mysql> create table think_bbs( -> id int not null primary key auto_increment, -> name char not null , -> content text not null, -> time char not null -> ) engine = myisam default charset=utf8;并且添加了一些测试数据进去 下面是show方法的代码 以及show.html的代码
public function show(){ #取出数据 $result = db('bbs')->select(); #分配给模板 $this->assign('result',$result); return $this->fetch(); } <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <!--获得数据库信息res--> {volist name='result' id='vo' empty="没有数据哟"} id:{$i} name:{$vo.name} content: {$vo.content} time:{$vo.time}<br/> {/volist} </body> </html>实现分页功能 上面页面显示出来的内容没有分页 在一页显示所有内容十分不美观 实现分页功能主要以下几步 select方法没有分页功能改为paginate()方法 当传入一个参数时 代表一页又几行数据 当传入两个参数时 第一个参数代表一页又几行数据 第二个参数代表检索出多少数据 把show 方法代码改成下面这样:
public function show(){ #取出数据 $result = db('bbs')->where('id','>','0')->paginate(10); #分配给模板 $this->assign('result',$result); return $this->fetch(); } }同时修改show.html代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Tp5简易留言板</title> <!--修改样式能够让页码从竖直编程横向的--> <style type="text/css"> .pagination li{ display: inline; padding-right: 20px; list-style: none; } ul>li{ list-style: none; } </style> </head> <body> <!--获得数据库信息res--> <!--官方提供的分页样式--> <div> <ul> {volist name='result' id='vo' empty="没有数据哟"} <li>id:{$vo.id} name:{$vo.name} content: {$vo.content} time:{$vo.time} </li> {/volist} </ul> </div> {$result->render()} </body> </html>就是在html循环的后面调用一下 render方法 Ajax无刷新分页 ajax.html代码如下;
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function showHint(str){ var xmlhttp; if(str.length==0){ document.getElementById('txtHint').innerHTML=""; return; } if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById('txtHint').innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","www.myweb.com/tp5/public/index/bbs/show?page="+str,true); xmlhttp.send(); } </script> </head> <body> <h1>留言展示-Tp5建议留言板</h1> 留言如下; <p><span id="txtHint"></span></p> <h3>在输入框输入页码</h3> <from action=""> 跳转到 :<input type="text" id="txt1" value="1" onkeyup="showHint(this.value)"/> </from> </body> </html>Bootstrap框架美化 html代码中引入bostrap框架 已经从官网下载好 并且放到了vendor 下面
!--bookstrap基于jquery实现--> <link rel="stylesheet" href="/tp5/vendor/bootstrap/bootstrap.min.css"> <script src="/tp5/vndor/jquery/jquery-1.12.4.js"></script> <script src="/tp5/vendor/jquery/bootstrap.min.js"></script>bootstrap会自动根据代码中class的值进行样式分配 BBS简单发布功能的实现 新建一个add方法用于处理发布功能
public function add(){ #获取表单数据 $name = input('param.title'); $content = input('param.content'); if(!empty($name)&&!empty($content)){ $db = db('bbs'); $db->insert(['name'=>"{$name}",'content'=>"{$content}",'time'=>time()]); return $this->success('留言成功','/tp5/public/index.php/index/bbs/show');//封装好的跳转页面 继承了Controller 可以直接使用 } return $this->fetch(); }需要注意的是 获取表单中的数据这里采用的是input(param.name);的方式 input只要传入param.表单的Name值 就可以获得数据 而不区分get与post方法 add.html代码如下
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Tp5简易留言板发布</title> <link rel="stylesheet" href="/tp5/vendor/bootstrap/bootstrap.min.css"> <script src="/tp5/vndor/jquery/jquery-1.12.4.js"></script> <script src="/tp5/vendor/jquery/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Tp5留言发布</h2> <p>发布留言</p> <!--action不填不会跳转到页面--> <form action="" method="POST"> <div class="form-group"> <label for="exampleInputName2">留言标题</label> <input type="text" class="form-control" id="title" name="title" value="" placeholder="请输入留言"> </div> <div class='form-group'> <label for="exampleInputpassword1">留言内容content;</label> <textarea class="form-control" rows="3" id="content" name="content" value="" placeholder="请输入留言内容"></textarea> </div> <button type="submit" class="btn btn-primary">发布留言</button> </form> </div> </body> </html>展示页字段处理 1 控制标题 不能让用户传任意长度的标题 配置php.ini文件 加载拓展库 并且配置mb_substr防止字符串乱码
mbstring.language = Chinese mbstring.internal_encoding = UTF-8 mbstring.encoding_translation = On mbstring.http_input = UTF-8 mbstring.http_output = UTF-8 mbstring.detect_order = UTF-8 mbstring.substitute_character = none配置好之后更改show.html
{$vo.name|mb_substr=0,20,'utf-8'}</td><td>{$vo.content|mb_substr=0,40,'utf-8'}显示页面调用截取mb_substr方法 2调整显示时间 更改add代码 让传入时候 就传入有格式的字符串
$time =date('y-m-d H:i',time()); $db->insert(['name'=>"{$name}",'content'=>"{$content}",'time'=>$time]);3按顺序显示 更改show代码 让查询出的数据 按降序排序 加入详情页功能 首先修改show.html代码使得点击标题可以调用控制球的view方法 并把此时的id 传给方法
<td><a href="/tp5/public/index.php/index/bbs/view/id/{$vo.id}">{$vo.name|mb_substr=0,20,'utf-8'}</a></td>然后在bbs控制器中新增view方法 代码如下:
public function view(){ #接受href标签传来的id值 $id = input('param.id'); if(!empty($id)){ $result = db('bbs')->where('id',$id)->select(); $this->assign('result',$result); }else{ return $this->error('没有数据'); } return $this->fetch(); }然后写详情页前端代码
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Tp5简易留言板详情页</title> <link rel="stylesheet" href="/tp5/vendor/bootstrap/bootstrap.min.css"> <script src="/tp5/vndor/jquery/jquery-1.12.4.js"></script> <script src="/tp5/vendor/jquery/bootstrap.min.js"></script> </head> <body> <div css="container"> <h2><a href="/tp5/public/index.php/index/bbs/show">Tp5简易留言板</a></h2> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">{$result.0.name}</h3> </div> <div class="panel-body"> {$result.0.content|default="这家伙很懒 什么也没留下"|} </div> <ul class="list-group"> <li class="list-group-item">{$result.0.time}</li> </ul> </div> </div> </body>此时项目代码就基本完成了 视图代码主要有add.html show.html以及上面的详情页view.html add.html:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Tp5简易留言板发布</title> <link rel="stylesheet" href="/tp5/vendor/bootstrap/bootstrap.min.css"> <script src="/tp5/vndor/jquery/jquery-1.12.4.js"></script> <script src="/tp5/vendor/jquery/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Tp5留言发布</h2> <p>发布留言</p> <!--action不填不会跳转到页面--> <form action="" method="POST"> <div class="form-group"> <label for="exampleInputName2">留言标题</label> <input type="text" class="form-control" id="title" name="title" value="" placeholder="请输入留言"> </div> <div class='form-group'> <label for="exampleInputpassword1">留言内容content;</label> <textarea class="form-control" rows="3" id="content" name="content" value="" placeholder="请输入留言内容"></textarea> </div> <button type="submit" class="btn btn-primary">发布留言</button> </form> </div> </body> </html>show.html:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Tp5简易留言板</title> <!--修改样式能够让页码从竖直编程横向的--> <style type="text/css"> .pagination li{ display: inline; padding-right: 20px; list-style: none; } ul>li{ list-style: none; } </style> <!--bookstrap基于jquery实现--> <link rel="stylesheet" href="/tp5/vendor/bootstrap/bootstrap.min.css"> <script src="/tp5/vndor/jquery/jquery-1.12.4.js"></script> <script src="/tp5/vendor/jquery/bootstrap.min.js"></script> </head> <body> <!--获得数据库信息res--> <!--官方提供的分页样式--> <div class="container"> <h1>留言展示-TP5简易留言板</h1> <p>留言如下:</p> <div> <table class="table table-bordered table-hover"> <thead> <tr class="danger"><th>ID</th><th>标题</th><th>内容</th><th>留言时间</th></tr></thead> <tbody> {volist name='result' id='vo' empty="没有数据哟"} <!--mb_substr方法需要加载拓展库 并配置 --> <tr class="success"><th>{$vo.id}</th><td><a href="/tp5/public/index.php/index/bbs/view/id/{$vo.id}">{$vo.name|mb_substr=0,20,'utf-8'}</a></td><td>{$vo.content|mb_substr=0,40,'utf-8'}</td><td>{$vo.time}</td></tr> {/volist} </tbody> </table> {$result->render()} <br/> <a href="/tp5/public/index.php/index/bbs/add">发布留言</a> </div> </body> </html>还有一个控制器 Bbs.php
<?php #放入控制器空间 namespace app\index\controller; #需要继承控制器才能使用fetch方法 use think\Controller; use think\Db; class Bbs extends Controller{ public function show(){ #取出数据 $result = db('bbs')->where('id','>','0')->order('id','desc')->paginate(10); #分配给模板 $this->assign('result',$result); return $this->fetch(); } public function add(){ #获取表单数据 $name = input('param.title'); $content = input('param.content'); if(!empty($name)&&!empty($content)){ $db = db('bbs'); $time =date('y-m-d H:i',time()); $db->insert(['name'=>"{$name}",'content'=>"{$content}",'time'=>$time]); return $this->success('留言成功','/tp5/public/index.php/index/bbs/show');//封装好的跳转页面 继承了Controller 可以直接使用 } return $this->fetch(); } public function view(){ #接受href标签传来的id值 $id = input('param.id'); if(!empty($id)){ $result = db('bbs')->where('id',$id)->select(); $this->assign('result',$result); }else{ return $this->error('没有数据'); } return $this->fetch(); } } ?>部署 准备一个云服务器以及一个备案解析好的域名 下载一个集成环境例如 studyphp(方便) 建一个数据库 bbs 以及表 think_bbs要能与上面的代码匹配 然后将整个tp5文件放入根目录 然后在默认的入口文件中做个网页跳转
header("locatio:http://www.blogwwm.com/tp5/public/index.php/index/bbs/show");这样的话就可以通过与域名· http://www.blogwwm.com 访问到留言板 顺便加上了背景音乐
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=0 height=0 src="//music.163.com/outchain/player?type=2&id=568434921&auto=1&height=66"></iframe>以及网站logo
<link rel="shortcut icon" href="/tp5/public/1.png" type="image/x-icon"> <link rel="icon" href="/tp5/public/1.png" type="image/x-icon">