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 app\index\model; use think\Model; 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 app\index\controller; 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 app\api\controller; class User { public function test() { return "sfdjkl非"; } }
保存后就可以通過:http://localhost/tp-demo/index.php/api/user/test來訪問了;