一,TP5的环境搭建
1.准备工具,去官网下载thinkPHP5,有各种班本的,建议下载完整版
百度搜索 “thinkphp”进入官网下载thinkphp5.0。
也可以利用Composer安装,git安装(官网下载较为方便)
2.进入phpStudy打开WWW文件
![](https://img.haomeiwen.com/i5815682/c26eb861edf57f76.png)
3.将下载好的文件放入新建文件并命名为tp5的文件中
![](https://img.haomeiwen.com/i5815682/29adf2f0e9024eb9.png)
4.进行测试,成功的标准为打开index.php后出现
![](https://img.haomeiwen.com/i5815682/ca86adda38cb0928.png)
这时你的一些相关配置已经配置好了
详情可以去百度进行搜索,这种文章很多
二.Tp5的运用
这个的前提是需要对mvc有一定了解的,下面是简单的实例
1.找到app文件,并在里面建立对应的文件夹
![](https://img.haomeiwen.com/i5815682/d4155925d18dde6b.png)
2.打开mysql建立数据库
![](https://img.haomeiwen.com/i5815682/9538da60dae9f066.png)
3.更改tp5里面的数据库文件的配置
![](https://img.haomeiwen.com/i5815682/2d72d6a4b19db802.png)
4.mvc里面的内容如下
控制层:
<?php
namespace app\index\controller;
use \think\Controller;
use app\index\model\Indexo as Log;
class Index extends Controller
{
public function index()
{
/// echo "string";
if (request()->isPost())
{
///实例化类
$indexo=new Log;
///进行判断,然后响应
$stats=$indexo->index(input("username"),input("password"));
if ($stats=1)
{
return $this->success("成功"."Index/index");
}
elseif ($stats=2)
{
return $this->error("用户名不存在");
}
else
{
return $this->error("请输入用户名和密码");
}
}
return $this->fetch("index");
}
public function test()
{
return "dsfgdfg";
}
}
模型层:
<?php
namespace app\index\model;
use \think\Model;
class Indexo extends Model
{
////传递两个参数
public function index($username,$password)
{
// echo "string";
$admin=\think\Db::name("good")->where("price","=",$username)->find();
if ($admin)
{
if ($admin['newproduct']=md5($password))
{
return 1;
}
else
{
return 2;
}
}
else
{
return 3;
}
}
}
view层:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<form action="" method="post">
用户名: <input type="text" name="username"><br>
密码: <input type="text" name="password" maxlength="4"><br>
<input type="reset" value="重置">
<input type="submit" value="提交">
</form>
<p>点击重置按钮重新设置表单。</p>
</body>
</html>
![](https://img.haomeiwen.com/i5815682/a05f098d3710766f.png)
总的来讲,这个过程不太难,如果遇到问题:将你的TP5改成debug模式,一般来讲主要遇到的问题有两个,一个是你的文件的路劲的问题,一个是你连接数据库的时候的问题,这两个问题如果遇到了,也不难,百度可以百度出很多结果,不过之所以开debug模式都是让你知道问题的所在,而且这两个问题,除非第二个的独具库连接那,否则,用不上百度!
![](https://img.haomeiwen.com/i5815682/62e8a2ff9aa2711a.png)
网友评论