本文我们将利用thinkphp创建一个简单的站点,这里所使用的thinkphp版本是5.0.24,这里是它的中文文档。如果有需要可以参考它的中文文档。

thinkphp框架是一个典型的MVC框架,该框架将网站的执行逻辑分为三层。第一层是模型层M,用于操作数据的底层逻辑,如测试数据的可行性、生成零时表等操作;第二层是控制层C,用于控制网站的执行逻辑;第三层是视图层V,即将显示在表层的网站页面存储为html文件,通过控制层传输需要的数据到视图层。

下面我们将利用thinkphp框架来搭建一个简单的网站,主要说明一些需要注意的技术点。

一、搭建开发环境

(一)安装php+mysql集成环境

1、这里推荐使用phpstudy来安装开发环境,到官网下载对应操作系统的phpstudy的安装包,安装到需要的位置即可,推荐安装到非C盘且容量较大的位置,方便管理网站的相关资源。

2、打开phpstudy的控制面板,打开apache和mysql服务,在浏览器输入 http://localhost 或者 http://127.0.0.1 ,能够打开页面说明web环境已安装成功。

3、可考虑安装phpmyadmin或者sqlyog等数据库管理工具。

ps:这里可能出现端口占用等问题,可考虑重启或改变端口等方案。

(二)配置环境

1、配置网站根目录。当前最新版本的phpstudy默认的网站根目录是安装目录下的/WWW文件夹。如果要改变这个根目录,请在“环境/Web Servers/基本配置/网站目录”中进行设置,选择所需网站根目录的位置。

2、在mysql管理软件中可查看数据库中现有的表,也可更改数据库用户名和密码,默认用户名和密码均为root。

3、注意thinkphp需要使用php5.3以上的版本,因此需安装较高版本的php环境并进行配置。

(三)下载thinkphp5

点击这里下载thinkphp5.0.24框架,将文件解压到网站的根目录,然后将文件夹重命名为tp5.0。会发现根目录下只有tp5.0一个文件夹。

(四)下载开发工具

这里我们推荐使用vscode对网站进行编辑,点击这里下载vscode。

二、thinkphp入门

(一)定义入口文件

此时如果访问 http://localhost/ 会出现:

1
2
3
Forbidden
You don't have permission to access / on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

但如果访问 http://localhost/tp5.0/public/ 将会得到正常的反馈。

现在我们要做的就是将入口文件移到根目录下,所以复制/tp5.0/public/index.php到网站根目录下,打开发现是如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';

现在我们要对文件结构进行修改,将tp5.0中的thinkphp、vendor、application三个文件夹复制到网站根目录,然后修改网站根目录下的index.php的代码为:

1
2
3
4
5
6
7
8
9
<?php
if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !');

// 引入ThinkPHP入口文件
define('APP_PATH', __DIR__ . './application/');
define("BIND_MODULE",'index');

// 加载框架引导文件
require __DIR__ . './thinkphp/start.php';

现在访问 http://localhost/ 将会出现正确的反馈。

(二)打开DEBUG模式方便调试

修改\application\config.php文件中的’app_debug’值为true,即其代码会显示为:

1
'app_debug'              => true,

这样就打开了调试模式。在部署环境中可将该值改为false。

(三)修改主页的返回值

默认主页控制器是\application\index\controller\Index.php文件控制的,因此打开该文件,发现该文件代码如下:

1
2
3
4
5
6
7
8
9
10
<?php
namespace app\index\controller;

class Index
{
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
}
}

我们修改代码如下:

1
2
3
4
5
6
7
8
9
10
<?php
namespace app\index\controller;

class Index
{
public function index()
{
return 'hello world';
}
}

然后访问 http://localhost/ 将会出现hello world的字样,说明主页修改成功。

三、使用thinkphp

(一)修改主页套用的模板文件

现在我们想让主页返回一个html文件,那么应该如何操作呢?

在\application\index文件夹中新建一个文件夹名为view,这就是视图层所在的文件夹,在view文件夹中再建一个名为index的文件夹,这就是\application\index\controller\index.php控制器对应的视图文件夹,在这里面我们需要建立一个index.html文件,我们修改该文件的代码如下所示:

1
2
3
4
5
6
7
8
<html>
<head>
<title>主页</title>
</head>
<body>
<input type="text"></input>
</body>
</html>

然后我们需要修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
namespace app\index\controller;

use think\Controller;
use think\View;

class Index extends Controller
{
public function index()
{
return $this->fetch();
}
}

使控制器调用该index.html文件。然后访问 http://localhost/ 会发现主页上出现了一个输入框,说明html文件调用成功。

(二)使用模板引擎进行传值

为了能让控制器输出不同的值到所见到的页面中,下面要用到thinkphp的模板引擎进行传值操作。

修改\application\index\view\index\index.html文件代码如下:

1
2
3
4
5
6
7
8
9
<html>
<head>
<title>主页</title>
</head>
<body>
<input type="text"></input>
hello,{$num}!
</body>
</html>

这里的{$num}就是要输出的值,然后修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
namespace app\index\controller;

use think\Controller;
use think\View;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
return $this->fetch();
}
}

然后访问 http://localhost/ 会发现主页上的值每次都不同,说明$num传值到html文件成功。

(三)访问链接

一个网站不可能只有一个链接,那么如何用视图中的html文件访问其他链接(也可以说是html文件)呢?这里我对它予以实现。让同学们了解控制器与链接的关系。

1、隐藏index.php

默认访问浏览器时会访问index.php。也就是所如果你在浏览器中直接输入 http://localhost/ 时会发现其访问的是index.php。为了让后面的链接更简洁我们这里做一步操作来隐藏index.php。在网站根目录下新建一个’.htaccess’的文件,该文件的作用就是让所有链接经过index.php。修改该文件的代码为:

1
2
3
4
5
6
7
8
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>

修改完成后将默认访问index.php

2、修改html文件

修改\application\index\view\index\index.html文件代码如下:

1
2
3
4
5
6
7
8
9
10
<html>
<head>
<title>主页</title>
</head>
<body>
<input type="text"></input>
hello,{$num}!
<a herf="{:url('index/index2');}"></a>
</body>
</html>

这里的{:url(‘index/index2’);}代表该链接将要访问index控制其中的index2方法。当然这里index2方法还没创建,我们现在来创建它。修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
namespace app\index\controller;

use think\Controller;
use think\View;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
return $this->fetch();
}
public function index2()
{
return 'index2';
}
}

现在我们访问 http://localhost/ 会出现一个链接,点击进去后会返回index2这个字符串,发现上面的浏览器地址也变为了 http://localhost/index/index2.html ,也就说明了链接跳转成功了。

这里我们不想要html这个后缀,怎么进行操作呢?这里的后缀名为伪静态后缀,我们需要在\application\config.php文件中进行修改,将:

1
2
// URL伪静态后缀
'url_html_suffix' => 'html',

改为

1
2
// URL伪静态后缀
'url_html_suffix' => '',

这时我们再访问这个链接地址就会变成 http://localhost/index/index2,这样就让链接更加简洁了。

(四)链接传值

我们现在想让链接附带参数,应该如何操作呢?修改\application\index\view\index\index.html文件代码如下:

1
2
3
4
5
6
7
8
9
10
<html>
<head>
<title>主页</title>
</head>
<body>
<input type="text"></input>
hello,{$num}!
<a herf="{:url('index/index2');}/num/{$num}"></a>
</body>
</html>

这样我们就在链接中添加了一个名为num的参数,我们的控制器如何获取它呢?

现在我们修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
namespace app\index\controller;

use think\Controller;
use think\View;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
return $this->fetch();
}
public function index2($num)
{
return $num;
}
}

注意在index2方法中加入了$num变量,index2方法就是返回它,这样在第二个链接中就会返回第一个链接中生成的随机数。这样就完成了链接传值的操作,这些传来的值就可以在控制器中做进一步处理,比如用作跳转特定id的页面、读取特定的数据等。

(五)添加静态资源

修改\application\config.php文件中的view_replace_str配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    // 视图输出字符串内容替换
'view_replace_str' => [
'__HOME__'=>'/public/static',
],
```
现在在视图文件中输入__HOME__即可代指静态文件所在的位置,可在其中添加相应的js及css文件。

## 四、连接数据库

### (一)创建mysql数据库

如何用thinkphp框架读取数据库中的数据呢?该框架提供了一种非常简单的语法,下面我对其入门操作做一个示范。

这里我们用phpmyadmin先建一个数据库和一个数据表,然后写入一些字段。

1、配置phpmyadmin

将下好的phpmyadmin放到网站根目录,在浏览器中访问 http://localhost/phpmyadmin/ 即可进入数据库管理界面。

输入用户名、密码,默认均为root。

2、新建数据库和数据表

单击'新建',输入数据库的名字,这里我们输入test作为测试数据库,右边的编码格式一般选为utf8_general_ci,单击'创建'完成创建工作。

创建完数据库后开始新建表,这里我们新建一个表名为user,字段数填为3,单击'执行'完成表的创建工作。

名字分别为id、name、pw,类型分别为INT、VARCHAR、VARCHAR,长度分别为8、20、20,索引分别为PRIMARY、空、空。其余不管,单击'保存'即可完成表的创建工作。

3、添加数据

现在我们在表中添加几组数据,我们单击'插入'选项卡,填写值为1、whl、123456,单击'执行'完成第一组数据的添加。

同样添加第二组数据分别为2、xxx、654321。

这样就完成了数据的添加,下面我们想要将其显示在网页中。

### (二)配置thinkphp数据库文件

修改\application\database.php配置如下:

``` bash
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'test',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
// 端口
'hostport' => '',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => '',
// 数据库调试模式
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 自动读取主库数据
'read_master' => false,
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型
'resultset_type' => 'array',
// 自动写入时间戳字段
'auto_timestamp' => false,
// 时间字段取出后的默认时间格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否需要进行SQL性能分析
'sql_explain' => false,
];

使其默认连接test数据库,并输入用户名、密码即可完成配置。

(三)输出数据表

下面我们来输出数据库中的数据,修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
namespace app\index\controller;

use think\Controller;
use think\View;
use think\Db;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
$DB=new Db;
$user=$DB::table("user")->select();
$this->assign('user',$user);
return $this->fetch();
}
public function index2($num)
{
return $num;
}
}

这里我们查找id为1的用户并输出。再修改\application\index\view\index\index.html文件代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<head>
<title>主页</title>
</head>
<body>
<input type="text"></input>
hello,{$num}!
<a href="{:url('index/index2');}/num/{$num}">访问index2</a>
<br>
{foreach $user as $data}
{$data.id}|{$data.name}|{$data.pw}<br>
{/foreach}
</body>
</html>

访问http://localhost/ 发现用户信息表已输出。注意到{foreach $user as $data}{/foreach}标签,这个标签标识将传来的$user值更名为$data,并循环输出。

(四)条件查询

现在我们来尝试添加条件查询,修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
namespace app\index\controller;

use think\Controller;
use think\View;
use think\Db;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
$DB=new Db;
$map['id']=1;
$user=$DB::table("user")->where($map)->select();
$this->assign('user',$user);
return $this->fetch();
}
public function index2($num)
{
return $num;
}
}

这样我们访问 http://localhost/ 时会发现仅输出了id为1的用户,完成了条件查询。

有时我们不用输出一张表,仅需输出一个值,那么应该如何操作呢?修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
namespace app\index\controller;

use think\Controller;
use think\View;
use think\Db;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
$DB=new Db;
$map['id']=1;
$user=$DB::table("user")->where($map)->select();
$this->assign('user',$user);
$user1=$DB::table("user")->where($map)->find();
$this->assign('user1',$user1);
return $this->fetch();
}
public function index2($num)
{
return $num;
}
}

这是我们查找id为1的用户并将其值进行输出,下面调整html代码。修改\application\index\view\index\index.html文件代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<title>主页</title>
</head>
<body>
<input type="text"></input>
hello,{$num}!
<a href="{:url('index/index2');}/num/{$num}">访问index2</a>
<br>
{foreach $user as $data}
{$data.id}|{$data.name}|{$data.pw}<br>
{/foreach}
{$user1.name}
</body>
</html>

这里的{$user1.name}就输出了id为1的用户的姓名信息。

(五)增加数据

为了能向数据库写入数据,我们做以下修改,修改\application\index\view\index\index.html文件代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<html>
<head>
<title>主页</title>
</head>
<body>
<form method="post" action="{:url('index/add');}">
<input type="text" name="id" placeholder="id"></input><br>
<input type="text" name="name" placeholder="name"></input><br>
<input type="text" name="pw" placeholder="pw"></input><br>
<input type="submit"></input><br>
</form>
<br>
hello,{$num}!
<br>
<a href="{:url('index/index2');}/num/{$num}">访问index2</a>
<br>
{foreach $user as $data}
{$data.id}|{$data.name}|{$data.pw}<br>
{/foreach}
{$user1.name}
</body>
</html>

这里我们创建了一个表单与数据库一一对应,然后修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace app\index\controller;

use think\Controller;
use think\View;
use think\Db;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
$DB=new Db;
$user=$DB::table("user")->select();
$this->assign('user',$user);
$map['id']=1;
$user1=$DB::table("user")->where($map)->find();
$this->assign('user1',$user1);
return $this->fetch();
}
public function index2($num)
{
return $num;
}
public function add()
{
if(request()->isPost()){
$data=[
'id'=>input('id'),
'name'=>input('name'),
'pw'=>input('pw'),
];
$DB=new Db;
$DB::name('user')->insert($data);
}
$this->success("添加成功!");
}
}
其中我们添加了一个add方法,提交数据后发现数据能够成功提交。

(六)删除数据

下面介绍一下如何删除数据,修改\application\index\view\index\index.html文件代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<html>
<head>
<title>主页</title>
</head>
<body>
<form method="post" action="{:url('index/add');}">
<input type="text" name="id" placeholder="id"></input><br>
<input type="text" name="name" placeholder="name"></input><br>
<input type="text" name="pw" placeholder="pw"></input><br>
<input type="submit"></input><br>
</form>
<br>
hello,{$num}!
<br>
<a href="{:url('index/index2');}/num/{$num}">访问index2</a>
<br>
{foreach $user as $data}
{$data.id}|{$data.name}|{$data.pw}|<a href="{:url('index/del');}/id/{$data.id}">删除</a><br>
{/foreach}
{$user1.name}
</body>
</html>

然后修改\application\index\controller\index.php文件代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace app\index\controller;

use think\Controller;
use think\View;
use think\Db;

class Index extends Controller
{
public function index()
{
$num=rand(5, 15);
$this->assign('num',$num);
$DB=new Db;
$user=$DB::table("user")->select();
$this->assign('user',$user);
$map['id']=1;
$user1=$DB::table("user")->where($map)->find();
$this->assign('user1',$user1);
return $this->fetch();
}
public function index2($num)
{
return $num;
}
public function add()
{
if(request()->isPost()){
$data=[
'id'=>input('id'),
'name'=>input('name'),
'pw'=>input('pw'),
];
$DB=new Db;
$DB::name('user')->insert($data);
}
$this->success("添加成功!");
}
public function del($id)
{
$DB=new Db;
$info=$DB::table("user")->where('id',$id)->delete();
if($info){
$this->success("删除成功!");
}else{
$this->error($file->getError());
}
}
}

其中添加了一个del方法,使用了del()函数,点击链接数据删除说明本步成功。

其余一些数据库的操作方法请参照这里来实现。

这样关于如何使用thinkphp来创建一些简单的网站的基本技术就大体说明完了,如果还有其他问题请参照
这里予以完成。