linux + 寶塔 + thinkphp5.0 搭建后端api各種問題集合


安裝寶塔

官方地址:https://www.bt.cn/bbs/thread-19376-1-1.html

我用的centos 7 所以有以下指令

yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh

安裝完畢,記下我們服務器的寶塔賬號和密碼

然后登陸寶塔,在軟件商店中安裝nginx + php,我用的雲數據庫,所以沒有安裝mysql

 然后我們去安裝thinkphp 5,官方地址:https://www.kancloud.cn/manual/thinkphp5/118006

Composer安裝

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

用composer安裝別忘了更改鏡像源,國內鏡像源比較快

composer config -g repo.packagist composer https://packagist.phpcomposer.com

然后到web根目錄下去執行

composer create-project topthink/think=5.0.* tp5  --prefer-dist

compser過程中putenv、proc_open被禁用和版本報錯

去寶塔的軟件商店設置一下php禁用函數

找到putenv然后刪除

同理,解除proc_open禁用

版本報錯

替換鏡像源即可

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

php啟動服務過程中passthru被禁用

在寶塔中解除禁用即可

Git安裝

親測,碼雲的這個已經不能用了

 直接用github的,稍微慢一點

git clone https://github.com/top-think/think tp5
git clone https://github.com/top-think/framework thinkphp

然后進入thinkphp目錄,切換核心庫到master分支

cd thinkphp
git checkout master
git pull https://github.com/top-think/framework

上述步驟結束,tp5就安裝完成了,然后我們去寶塔配置下nginx

到網站選項添加站點

點設置打開配置文件

在51行添加以下代碼來支持tp的pathinfo

location / {
    try_files $uri $uri/ /index.php$is_args$args;
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?s=$1 last;
        break;
    }
}

 如果有證書的話,下載下來配置ssl

然后去網站目錄更改運行目錄為框架低下的public目錄

至此寶塔這部分結束

tp5在linux下的權限問題

進入主目錄,將runtime目錄設置為777,注意-R一定是大寫的R,不然無法執行

chmod -R 777 runtime/

然后將public目錄設置為755,如果運行有問題,則將其設置為755

chmod -R 755 public/

沒有意外情況出現的話,tp5可以正常運行

如果還運行不了,將整個工程設置為777,然后重復上述兩步

數據庫配置

tp5支持解析帶端口號的數據庫地址

根據實際情況配置,單一庫或者主從庫

<?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'        => 'bj-cdb-nivnaury.sql.tencentcdb.com:60991',
    // 數據庫名
    'database'        => 'lianmai',
    // 用戶名
    'username'        => 'username',
    // 密碼
    'password'        => 'password',
    // 端口
    'hostport'        => '60991',
    // 連接dsn
    'dsn'             => '',
    // 數據庫連接參數
    'params'          => [],
    // 數據庫編碼默認采用utf8
    'charset'         => 'utf8',
    // 數據庫表前綴
    'prefix'          => '',
    // 數據庫調試模式
    'debug'           => true,
    // 數據庫部署方式:0 集中式(單一服務器),1 分布式(主從服務器)
    'deploy'          => 0,
    // 數據庫讀寫是否分離 主從式有效
    'rw_separate'     => true,
    // 讀寫分離后 主服務器數量
    '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,
    //'unix_socket' => '/var/run/mysqld/mysqld.sock'
];

數據遷移工具Migration

https://www.cnblogs.com/YC-L/p/12635731.html

跨域處理

在應用生命周期的開頭注冊下面的跨域類

use think\Response;
 
class CORS
{
    public function appInit(&$params)
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, Content-Type, Accept");
        header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
        if (request()->isOptions()) {
            exit();
        }
    }
}

tp5.0應用生命周期

tags.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>
// +----------------------------------------------------------------------

// 應用行為擴展定義文件
return [
    // 應用初始化
    'app_init'     => [
        'app\\api\\behavior\\CORS'
    ],
    // 應用開始
    'app_begin'    => [],
    // 模塊初始化
    'module_init'  => [],
    // 操作開始執行
    'action_begin' => [],
    // 視圖內容過濾
    'view_filter'  => [],
    // 日志寫入
    'log_write'    => [],
    // 應用結束
    'app_end'      => [],
];

Jwt的使用

github地址:https://github.com/lcobucci/jwt

composer安裝

composer require lcobucci/jwt

然后寫令牌

<?php
namespace app\admin\token;

use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\ValidationData;

class Token
{    
    // 生成jwt
    public static function token($uid = '', $user_name = ''){        

            $signer = new Sha256();

            $token = (new Builder())->setIssuer('https://sqadmin.dao-tech.com')
                ->setAudience('https://sqadmin.dao-tech.com')
                ->setId($uid . $user_name, true) //自定義標識
                ->setIssuedAt(time()) 
                ->setExpiration(time() + (86400 * 30)) //token有效期時長
                ->set('uid', $uid)
                ->sign($signer, 'llwhappyeveryday')
                ->getToken();

            return (String) $token;     
     }        

    // 驗證jwt
    public static function check($token, $uid, $user_name) 
    {
        $token = (new Parser())->parse((String) $token);

        $signer =  new Sha256();

        if (!$token->verify($signer, 'llwhappyeveryday')) {
            return false; //簽名不正確
        }

        $validationData = new ValidationData();

        $validationData->setIssuer('https://sqadmin.dao-tech.com');
        $validationData->setAudience('https://sqadmin.dao-tech.com');
        $validationData->setId($uid . $user_name);//自定義標識

        return $token->validate($validationData);
    }
}

應用配置 config.php

常用的配置

  • 應用模式
  • 路由緩存
  • pathinfo配置
  • 分頁
  • session和cookie
  • 自定義的全局變量
<?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 [
    // +----------------------------------------------------------------------
    // | 應用設置
    // +----------------------------------------------------------------------

    // 應用調試模式
    'app_debug'              => true,
    // 應用Trace
    'app_trace'              => false,
    // 應用模式狀態
    'app_status'             => '',
    // 是否支持多模塊
    'app_multi_module'       => true,
    // 入口自動綁定模塊
    'auto_bind_module'       => false,
    // 注冊的根命名空間
    'root_namespace'         => [],
    // 擴展函數文件
    'extra_file_list'        => [THINK_PATH . 'helper' . EXT],
    // 默認輸出類型
    'default_return_type'    => 'html',
    // 默認AJAX 數據返回格式,可選json xml ...
    'default_ajax_return'    => 'json',
    // 默認JSONP格式返回的處理方法
    'default_jsonp_handler'  => 'jsonpReturn',
    // 默認JSONP處理方法
    'var_jsonp_handler'      => 'callback',
    // 默認時區
    'default_timezone'       => 'PRC',
    // 是否開啟多語言
    'lang_switch_on'         => false,
    // 默認全局過濾方法 用逗號分隔多個
    'default_filter'         => '',
    // 默認語言
    'default_lang'           => 'zh-cn',
    // 應用類庫后綴
    'class_suffix'           => false,
    // 控制器類后綴
    'controller_suffix'      => false,

    // +----------------------------------------------------------------------
    // | 模塊設置
    // +----------------------------------------------------------------------

    // 默認模塊名
    'default_module'         => 'index',
    // 禁止訪問模塊
    'deny_module_list'       => ['common'],
    // 默認控制器名
    'default_controller'     => 'Index',
    // 默認操作名
    'default_action'         => 'index',
    // 默認驗證器
    'default_validate'       => '',
    // 默認的空控制器名
    'empty_controller'       => 'Error',
    // 操作方法后綴
    'action_suffix'          => '',
    // 自動搜索控制器
    'controller_auto_search' => false,

    // +----------------------------------------------------------------------
    // | URL設置
    // +----------------------------------------------------------------------

    // PATHINFO變量名 用於兼容模式
    'var_pathinfo'           => 's',
    // 兼容PATH_INFO獲取
    'pathinfo_fetch'         => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],
    // pathinfo分隔符
    'pathinfo_depr'          => '/',
    // URL偽靜態后綴
    'url_html_suffix'        => 'html|json|xml',
    // URL普通方式參數 用於自動生成
    'url_common_param'       => false,
    // URL參數方式 0 按名稱成對解析 1 按順序解析
    'url_param_type'         => 0,
    // 是否開啟路由
    'url_route_on'           => true,
    // 路由使用完整匹配
    'route_complete_match'   => false,
    // 路由配置文件(支持配置多個)
    'route_config_file'      => ['route'],
    // 是否開啟路由解析緩存
    'route_check_cache'      => false,
    // 是否強制使用路由
    'url_route_must'         => true,
    // 域名部署
    'url_domain_deploy'      => false,
    // 域名根,如thinkphp.cn
    'url_domain_root'        => '',
    // 是否自動轉換URL中的控制器和操作名
    'url_convert'            => true,
    // 默認的訪問控制器層
    'url_controller_layer'   => 'controller',
    // 表單請求類型偽裝變量
    'var_method'             => '_method',
    // 表單ajax偽裝變量
    'var_ajax'               => '_ajax',
    // 表單pjax偽裝變量
    'var_pjax'               => '_pjax',
    // 是否開啟請求緩存 true自動緩存 支持設置請求緩存規則
    'request_cache'          => false,
    // 請求緩存有效期
    'request_cache_expire'   => null,
    // 全局請求緩存排除規則
    'request_cache_except'   => [],

    // +----------------------------------------------------------------------
    // | 模板設置
    // +----------------------------------------------------------------------

    'template'               => [
        // 模板引擎類型 支持 php think 支持擴展
        'type'         => 'Think',
        // 默認模板渲染規則 1 解析為小寫+下划線 2 全部轉換小寫
        'auto_rule'    => 1,
        // 模板路徑
        'view_path'    => '',
        // 模板后綴
        'view_suffix'  => 'html',
        // 模板文件名分隔符
        'view_depr'    => DS,
        // 模板引擎普通標簽開始標記
        'tpl_begin'    => '{',
        // 模板引擎普通標簽結束標記
        'tpl_end'      => '}',
        // 標簽庫標簽開始標記
        'taglib_begin' => '{',
        // 標簽庫標簽結束標記
        'taglib_end'   => '}',
    ],

    // 視圖輸出字符串內容替換
    'view_replace_str'       => [],
    // 默認跳轉頁面對應的模板文件
    'dispatch_success_tmpl'  => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
    'dispatch_error_tmpl'    => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',

    // +----------------------------------------------------------------------
    // | 異常及錯誤設置
    // +----------------------------------------------------------------------

    // 異常頁面的模板文件
    'exception_tmpl'         => THINK_PATH . 'tpl' . DS . 'think_exception.tpl',

    // 錯誤顯示信息,非調試模式有效
    'error_message'          => '頁面錯誤!請稍后再試~',
    // 顯示錯誤信息
    'show_error_msg'         => false,
    // 異常處理handle類 留空使用 \think\exception\Handle
    'exception_handle'       => '',

    // +----------------------------------------------------------------------
    // | 日志設置
    // +----------------------------------------------------------------------

    'log'                    => [
        // 日志記錄方式,內置 file socket 支持擴展
        'type'  => 'File',
        // 日志保存目錄
        'path'  => LOG_PATH,
        // 日志記錄級別
        'level' => [],
    ],

    // +----------------------------------------------------------------------
    // | Trace設置 開啟 app_trace 后 有效
    // +----------------------------------------------------------------------
    'trace'                  => [
        // 內置Html Console 支持擴展
        'type' => 'Html',
    ],

    // +----------------------------------------------------------------------
    // | 緩存設置
    // +----------------------------------------------------------------------

    'cache'                  => [
        // 驅動方式
        'type'   => 'File',
        // 緩存保存目錄
        'path'   => CACHE_PATH,
        // 緩存前綴
        'prefix' => '',
        // 緩存有效期 0表示永久緩存
        'expire' => 0,
    ],

    // +----------------------------------------------------------------------
    // | 會話設置
    // +----------------------------------------------------------------------

    'session'                => [
        'id'             => '',
        // SESSION_ID的提交變量,解決flash上傳跨域
        'var_session_id' => '',
        // SESSION 前綴
        'prefix'         => 'think',
        // 驅動方式 支持redis memcache memcached
        'type'           => '',
        // 是否自動開啟 SESSION
        'auto_start'     => true,
    ],

    // +----------------------------------------------------------------------
    // | Cookie設置
    // +----------------------------------------------------------------------
    'cookie'                 => [
        // cookie 名稱前綴
        'prefix'    => '',
        // cookie 保存時間
        'expire'    => 0,
        // cookie 保存路徑
        'path'      => '/',
        // cookie 有效域名
        'domain'    => '',
        //  cookie 啟用安全傳輸
        'secure'    => false,
        // httponly設置
        'httponly'  => '',
        // 是否使用 setcookie
        'setcookie' => true,
    ],

    //分頁配置
    'paginate'               => [
        'type'      => 'bootstrap',
        'var_page'  => 'page',
        'list_rows' => 15,
    ],
    // 小程序信息設置
    'wxapp' => [
        'appid' =>'',
        'appsecret'=>''
    ],
    'aliyun_access'=>[
        'accessKeyID'=>'',
        'accessKeySecret'=>''
    ]
];

路由配置

定義路由

use think\Route;
// 注冊路由到index模塊的News控制器的read操作
Route::rule('new/:id','index/News/read');

帶提交方式

Route::rule('new/:id','News/update','POST');

 直接使用提交方式的函數

Route::get('new/:id','News/read'); // 定義GET請求路由規則
Route::post('new/:id','News/update'); // 定義POST請求路由規則
Route::put('new/:id','News/update'); // 定義PUT請求路由規則
Route::delete('new/:id','News/delete'); // 定義DELETE請求路由規則
Route::any('new/:id','News/read'); // 所有請求都支持的路由規則

同一個請求,多種提交方式

Route::rule('new/:id','News/read','GET|POST');

使用數組批量添加路由

Route::rule([
    'new/:id'  =>  'News/read',
    'blog/:id' =>  ['Blog/update',['ext'=>'shtml'],['id'=>'\d{4}']],
    ...
],'','GET',['ext'=>'html'],['id'=>'\d+']);

變量規則

全局變量

// 設置name變量規則(采用正則定義)
Route::pattern('name','\w+');
// 支持批量添加
Route::pattern([
    'name'  =>  '\w+',
    'id'    =>  '\d+',
]);

局部變量

// 定義GET請求路由規則 並設置name變量規則
Route::get('new/:name','News/read',[],['name'=>'\w+']);

完整url規則

// 定義GET請求路由規則 並設置完整URL變量規則
Route::get('new/:id','News/read',[],['__url__'=>'new\/\w+$']);

路由參數

method 請求類型檢測,支持多個請求類型
ext URL后綴檢測,支持匹配多個后綴
deny_ext URL禁止后綴檢測,支持匹配多個后綴
https 檢測是否https請求
domain 域名檢測
before_behavior 前置行為(檢測)
after_behavior 后置行為(執行)
callback 自定義檢測方法
merge_extra_vars 合並額外參數
bind_model 綁定模型(V5.0.1+
cache 請求緩存(V5.0.1+
param_depr 路由參數分隔符(V5.0.2+
ajax Ajax檢測(V5.0.2+
pjax Pjax檢測(V5.0.2+

路由地址定義

方式1:路由到模塊/控制器 '[模塊/控制器/操作]?額外參數1=值1&額外參數2=值2...'
方式2:路由到重定向地址 '外部地址'(默認301重定向) 或者 ['外部地址','重定向代碼']
方式3:路由到控制器的方法 '@[模塊/控制器/]操作'
方式4:路由到類的方法 '\完整的命名空間類::靜態方法' 或者 '\完整的命名空間類@動態方法'
方式5:路由到閉包函數 閉包函數定義(支持參數傳入)

可以用路由到控制器方法,這種不用二次解析路由

'blog/:id'=>'@index/blog/read'

系統會直接執行

Loader::action('index/blog/read');

路由到類方法

'blog/:id'=>'\app\index\service\Blog@read'

重定向地址使用動態變量即可

'blog/:id'=>'http://blog.thinkphp.cn/read/:id'

路由別名

// user 別名路由到 index/User 控制器
Route::alias('user','index/User');

在route.php種定義

return [
    '__alias__' =>  [
        'user'  =>  'index/User',
    ],
];

路由別名可以指向任意一個有效的路由地址,例如下面指向一個類

// user 路由別名指向 User控制器類
Route::alias('user','\app\index\controller\User');

路由別名設置路由條件

// user 別名路由到 index/user 控制器
Route::alias('user','index/user',['ext'=>'html']);

在route.php中配置

return [
    '__alias__' =>  [
        'user'  =>  ['index/user',['ext'=>'html']],
    ],
];

操作方法黑白名單

路由別名的操作方法支持白名單或者黑名單機制,例如:

// user 別名路由到 index/user 控制器
Route::alias('user','index/user',[
    'ext'=>'html''allow'=>'index,read,edit,delete',
]);

或者使用黑名單機制

// user 別名路由到 index/user 控制器
Route::alias('user','index/user',[
    'ext'=>'html''except'=>'save,delete',
]);

並且支持設置操作方法的請求類型,例如:

// user 別名路由到 index/user 控制器
Route::alias('user','index/user',[
    'ext'=>'html''allow'=>'index,save,delete',
    'method'=>['index'=>'GET','save'=>'POST','delete'=>'DELETE'],
]);

路由分組

'blog/:id'   => ['Blog/read', ['method' => 'get'], ['id' => '\d+']],
'blog/:name' => ['Blog/read', ['method' => 'post']],

可以合並到一個blog分組

'[blog]'     => [
    ':id'   => ['Blog/read', ['method' => 'get'], ['id' => '\d+']],
    ':name' => ['Blog/read', ['method' => 'post']],
],

可以使用Group類的group方法

Route::group('blog',[
    ':id'   => ['Blog/read', ['method' => 'get'], ['id' => '\d+']],
    ':name' => ['Blog/read', ['method' => 'post']],
]);

路由分組支持嵌套

Route::group(['method'=>'get','ext'=>'html'],function(){
    Route::group('blog',function(){
        Route::any('blog/:id','blog/read',[],['id'=>'\d+']);
        Route::any('blog/:name','blog/read',[],['name'=>'\w+']);
    }
});

全部miss路由

return [
    'new/:id'   => 'News/read',
    'blog/:id'  => ['Blog/update',['method' => 'post|put'], ['id' => '\d+']],
    '__miss__'  => 'public/miss',
];

也可以使用miss方法

Route::miss('public/miss');

分組miss路由

return [
    '[blog]' =>  [
        'edit/:id'  => ['Blog/edit',['method' => 'get'], ['id' => '\d+']],
        ':id'       => ['Blog/read',['method' => 'get'], ['id' => '\d+']],
        '__miss__'  => 'blog/miss',
    ],
    'new/:id'   => 'News/read',
    '__miss__'  => 'public/miss',
];

在group方法中嵌套miss方法

Route::group('blog',function(){
    Route::rule(':id','blog/read',[],['id'=>'\d+']);
    Route::rule(':name','blog/read',[],['name'=>'\w+']);
    Route::miss('blog/miss');
},['method'=>'get','ext'=>'html']);

路由閉包函數

Route::get('hello/:name',function($name){ 
    return 'Hello,'.$name;
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM