1.先通过sudo apt install composer安装一下PHP的包管理工具;然后切换为国内镜像:composer config -g repo.packagist composer https://packagist.phpcomposer.com
2.下载ThinkPHP,命令:composer create-projec topthink/think:5.1.* tp5【tp5是项目名】
3.cd 到tp5目录,这个目录就是下载的ThinkPHP框架项目,然后直接在这个框架上添加自己的代码即可;
4.nginx配置:(tp5项目目录为/home/silentdoer/tp5)
location ^~ /tp-demo/ { set $path_info ""; set $real_script_name $fastcgi_script_name; if ($fastcgi_script_name ~ "/tp-demo/(.+.php)(/?.*)$") { set $real_script_name $1; set $path_info $2; } fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/silentdoer/tp5/public/$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; include fastcgi_params; }
然后reload nginx后就可以访问浏览器:http://localhost/tp-demo/看到页面上有ThinkPHP V5.1等字符串,说明配置成功;
5.开启debug配置便于调试:
修改config/app.php,将'app_debug' => false改成true
6.可以用VScode打开tp5目录,然后打开route目录里有route.php文件,里面有
<?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> // +---------------------------------------------------------------------- Route::get('think', function () { return 'hello,ThinkPHP5!'; }); Route::get('hello/:name', 'index/hello'); return [ ];
所以这里已经配置了路由(但是不配置也是可以的,这类似配置路由别名)可以测试了,在浏览器打开:http://localhost/tp-demo/index.php/think和http://localhost/tp-demo/index.php/index/index/index和http://localhost/tp-demo/index.php/index/index/hello均能看到返回(具体看application/index/controller/index.php的代码);由于存在别名,还可以用:http://localhost/tp-demo/index.php/hello/sf,它会转发到index/hello来处理;
6.自定义一个Controller(其实就是跟着这已经存在的画瓢就行了)并对外提供接口:
6.1.先在application/index下创建model目录,然后在model目录里添加User.php(注意必须是User.php不能是user.php,不知道这是框架的规范还是PHP的。。),然后User.php的代码是:
<?php namespace appindexmodel; use thinkModel; class User extends Model { public function getUsers($uid = 1) { // 模拟从数据库里获取数据 return array('name' => '王立奇', 'age' => 18); } }
6.2在index/controller里添加Blog.php(注意必须是Blog.php不能是BlogController.php或blog_controller.php或blog.php【当然这个可以配置,不过咱们和默认的Index.php规范一致即可】),代码为:
<?php // 定义此文件代码所在的命名空间 namespace appindexcontroller; class Blog { public function load() { $model = model('User'); $data = $model->getUsers(3); if ($data) { $code = 200; } else { $code = 404; } $data = [ 'code' => $code, 'data' => $data ]; return json($data); } }
6.3.可以在route/route.php里增加路由映射,但是也可以不加,不加通过访问:http://localhost/tp-demo/index.php/index/blog/load就能获得load()返回的json数据;
7.创建一个api Controller,首先在application目录下创建api目录,和index目录平级,然后在api目录里创建controller目录,里面创建User.php文件,然后其代码为:
<?php namespace appapicontroller; class User { public function test() { return "sfdjkl非"; } }
保存后就可以通过:http://localhost/tp-demo/index.php/api/user/test来访问了;