简书链接:php使用thinkphp50构建restfulapi之路第一篇
文章字数:1595,阅读全文大约需要6分钟
所谓restful api就是不包含index.php参数作为路径,然后参数有的则用json传递在里面,支持put,get,postdelete,
所以为了让我以前app写的架构能用,找了好半天才找到了毕竟好上手的。本人几年没玩php了,连sql语法差不多忘光了只会select * from xxx了,shit
需要配置一些东西 术语包含路由配置,隐藏入口文件 的入口文件index.php在 /tp5/public.index.php 那么.htaccess文件也应该和它在一起 也可以把这个文件弄到根目录,只是需要调整一下才行了.

所谓隐藏入口文件就是类似 访问 tp.com/index.php/index/index/index
变成tp.com/index/index/index/index
更多百度搜索
、在应用入口文件同级目录添加.htaccess文件,内容如下:
.htacess文件内容如下:

1
2
3
4
5
6
7
8
9
<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>

还需要开启 重写功能,
httpd.conf文件中 AllowOverride NoneNone改为 All另外开启一个模块
LoadModule rewrite_module modules/mod_rewrite.so
`

有没有生效可以直接这样测试如果 连 index.php都必须加上的话说明 没有配置好,没有配置好怎么能让
路由 http://tp5.com/index.php/index/index/hello
指向http://tp5.com/hello呢,因此这里尤为重要!

另外 http://127.0.0.1 默认指向了www文件夹
而 访问应该是http://127.0.0.1/tp5/public/index.php 才能访问到入口文件
整个路径就是非常非常长了, http://127.0.0.1/tp5/public/index.php/index/index/index 访问
D:\phpStudy\WWW\tp5\application\index\controller\index.php
中的index方法 首先就应该配置虚拟主机了,我使用的是phpstudy`
那么配置方式毕竟简单 ,编辑文件vhosts.conf

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
Listen 80
<VirtualHost _default_:9096>
DocumentRoot "D:\phpStudy\WWW"
<Directory "D:\phpStudy\WWW">
Options -Indexes +FollowSymLinks +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>


<VirtualHost *:80>
DocumentRoot "D:\phpStudy\WWW\tp5\public"
ServerName demo.cn
ServerAlias www.demo.cn
<Directory "D:\phpStudy\WWW\tp5\public">
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>

window中 本地域名
C:\Windows\System32\drivers\etc host文件添加

1
2
3
127.0.0.1 demo.cn
127.0.0.1 tp5.com

使用ping tp5.com可以验证结果

关于路由的探讨

现在tp5.com或者tp5.com/index 或者tp5.com/index.php 或者 tp5.com/index/index/index 的方法访问
/application/index/controller/index.phpindex方法 但是 如果和index.php同级的 qssq.php我怎么添加路由 实现tp5.com/qssq 访问这个文件都不行
!!

只能通过tp5.com/index/qssq/index/index访问 我说的 同级目录也就是说在路由指向/application/index/controller/qssq.php

最终没搞定 不过有2种方法倒是可以实现

  1. 是在application直接添加qssq/controller/index.php的模式 不知道啥术语,
  2. index/controller/index.php添加qssq方法然后路由指定。//Route::rule('qssq','index/Index/index/qssq');但是我有强迫症咋办,

注意

路由配置 方法的时候 申明了参数的时候,那么找不到路由的情况就要仔细看看是不是这个问题。

终于搞定之前的问题

注意

路由配置 方法的时候 申明了参数的时候,那么找不到路由的情况就要仔细看看是不是这个问题。

最后解决了这个问题 ,路由设置方式是:
Route::get('qssq','index/qssq/start');
真实路径是http://tp5.com/index/qssq/start

也就是说方法名最好别用index,否则会出现有时候就http://tp5.com/index/qssq/index 都访问不了的情况.

http://tp5.com/index/qssq/start 结构分析

start方法名,qssq php文件名,index, application里面的 index文件夹模块

默认的index 模块的idnex文件是http://tp5.com/index/index/index

快速生成restapi 方法 控制器

生成控制器又刚好在恰当的位置,选用index控制器也就是和index/controller/index.php文件在一起, 也就是上面我说的 通过路由 实现·tp5.com/qssq’访问 ‘application/index/controller/qssq.php`文件的某个方法的这种。

生成控制器得用php命令
经过我的研究发现,这样输入姿势才是最正确的,否则会出现各种情况不对,目录没没放对的情况.
先大致看一下我的项目结构,www文件夹下面有这些,

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
├─thinkphp
│ ├─lang
│ ├─library
│ │ ├─think
│ │ │ ├─cache
│ │ │ │ └─driver
│ │ │ ├─config
│ │ │ │ └─driver
│ │ │ ├─console
│ │ │ │ ├─bin
│ │ │ │ ├─command
│ │ │ │ │ ├─make
│ │ │ │ │ │ └─stubs
│ │ │ │ │ └─optimize
│ │ │ │ ├─input
│ │ │ │ └─output
│ │ │ │ ├─descriptor
│ │ │ │ ├─driver
│ │ │ │ ├─formatter
│ │ │ │ └─question
│ │ │ ├─controller
│ │ │ ├─db
│ │ │ │ ├─builder
│ │ │ │ ├─connector
│ │ │ │ └─exception
│ │ │ ├─debug
│ │ │ ├─exception
│ │ │ ├─log
│ │ │ │ └─driver
│ │ │ ├─model
│ │ │ │ └─relation
│ │ │ ├─paginator
│ │ │ │ └─driver
│ │ │ ├─process
│ │ │ │ ├─exception
│ │ │ │ └─pipes
│ │ │ ├─response
│ │ │ ├─session
│ │ │ │ └─driver
│ │ │ ├─template
│ │ │ │ ├─driver
│ │ │ │ └─taglib
│ │ │ └─view
│ │ │ └─driver
│ │ └─traits
│ │ ├─controller
│ │ ├─model
│ │ └─think
│ └─tpl
├─tp5
│ ├─application
│ │ ├─index
│ │ │ ├─controller
│ │ │ ├─model
│ │ │ └─view
│ │ └─my
│ │ └─controller
│ ├─client
│ ├─extend
│ ├─public
│ │ └─static
│ ├─runtime
│ │ └─log
│ │ └─201709
│ └─vendor
└─vendor
├─composer
└─topthink
└─think-installer
└─src

原来的Rest类继承的姿势已经不推荐了,所以我这里研究新姿势,
首先cmd命令行进入类似这样的目录`D:\phpStudy\www\tp5文件夹
输入

1
php think make:controller index/Qssq1

这时候会自动产生一个规范的文件
D:\phpStudy\www\tp5\application\index\controller\Qssq1.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
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
<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Qssq1 extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
}

/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}

/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}

/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}

/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}

/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
}

也就是下面这些

1
2
3
4
5
6
7
8
9

请求类型 生成路由规则 对应操作方法
GET blog index
GET blog/create create
POST blog save
GET blog/:id read
GET blog/:id/edit edit
PUT blog/:id update
DELETE blog/:id delete

然后你只需要为资源控制器注册一个资源路由:

Route::resource('qssq1','index/Qssq1');
为了方便测试在index方法返回了一段字符串 也在add里面返回了字符串,
然后分别访问tp5.com/qssq1 tp5.com/qssq1\add 发现完美搞定,那么 ?问号后面的参数如何接受呢.类似要高仿一个下面这样的api,
https://cloud.qssq.cn/mcm/api/devices?filter=%7B%22where%22%3A%7B%22account%22%3A%22694886526%22%7D%7D
也就是根的查询.

2019-5-23 19:24:24

再次玩这个玩意又变成了新鲜的了,各种错误,比如 没有指定文件是因为httpaccess文件,找不到文件 访问拒绝其实不是权限问题,而是文件不完整。

使用的最佳方法是从thinkphp官网下载 一个zip上传到服务器然后解压,然后 更改application/的 路由.php和 database.php就大功告成了。
thinkphp apache指定thinkphp的public文件夹就行,
控制器找不到要检查一下类名是否正常,我创建一个新的表,拷贝一个新的文件发现控制器不存在,我发现类名没改,所以改一下解决了问题。