快速搭建TP6-02
1.配置多应用config/app.php
return [
// 应用地址
'app_host' => env('app.host', ''),
// 应用的命名空间
'app_namespace' => '',
// 是否启用路由
'with_route' => true,
// 默认应用
'default_app' => 'Login',
// 默认时区
'default_timezone' => 'Asia/Shanghai',
//多应用
'auto_multi_app' => true,
// 应用映射(自动多应用模式有效)
'app_map' => [],
// 域名绑定(自动多应用模式有效)
'domain_bind' => [],
// 禁止URL访问的应用列表(自动多应用模式有效)
'deny_app_list' => [],
// 异常页面的模板文件
'exception_tmpl' => app()->getThinkPath() . 'tpl/think_exception.tpl',
// 错误显示信息,非调试模式有效
'error_message' => '页面错误!请稍后再试~',
// 显示错误信息
'show_error_msg' => false,
];
2.配置数据库config/database.php
return [
// 默认使用的数据库连接配置
'default' => env('database.driver', 'mysql'),
// 自定义时间查询规则
'time_query_rule' => [],
// 自动写入时间戳字段
// true为自动识别类型 false关闭
// 字符串则明确指定时间字段类型 支持 int timestamp datetime date
'auto_timestamp' => true,
// 时间字段取出后的默认时间格式
'datetime_format' => 'Y-m-d H:i:s',
// 数据库连接配置信息
'connections' => [
'mysql' => [
// 数据库类型
'type' => env('database.type', 'mysql'),
// 服务器地址
'hostname' => env('database.hostname', '118.178.123.186'),
// 数据库名
'database' => env('database.database', 'sc_dev'),
// 用户名
'username' => env('database.username', 'root'),
// 密码
'password' => env('database.password', '1234SA!@#$qianHUwanHUankuaisdk@@'),
// 端口
'hostport' => env('database.hostport', '3306'),
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => env('database.charset', 'utf8'),
// 数据库表前缀
'prefix' => env('database.prefix', 'sc_'),
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 是否需要断线重连
'break_reconnect' => false,
// 监听SQL
'trigger_sql' => env('app_debug', true),
// 开启字段缓存
'fields_cache' => false,
],
// 更多的数据库配置信息
],
];
3.修改session默认时间config/session.php
return [
// session name
'name' => 'PHPSESSID',
// SESSION_ID的提交变量,解决flash上传跨域
'var_session_id' => '',
// 驱动方式 支持file cache
'type' => 'file',
// 存储连接标识 当type使用cache的时候有效
'store' => null,
// 过期时间
'expire' => 86400,
// 前缀
'prefix' => '',
];
开启session middleware.php
<?php
// 全局中间件定义文件
return [
// 全局请求缓存
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
\think\middleware\SessionInit::class
];
4.自定义图片上传位置config/filesystem.php
return [
// 默认磁盘
'default' => env('filesystem.driver', 'local'),
// 磁盘列表
'disks' => [
'local' => [
'type' => 'local',
'root' => app()->getRuntimePath() . 'storage',
],
'public' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
'root' => app()->getRootPath() . 'public/uploads',
// 磁盘路径对应的外部URL路径
'url' => '/uploads',
// 可见性
'visibility' => 'public',
],
// 更多的磁盘配置信息
],
];
5.添加redis配置config/cache.php
return [
// 默认缓存驱动
'default' => env('cache.driver', 'file'),
// 缓存连接方式配置
'stores' => [
'file' => [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => '',
// 缓存前缀
'prefix' => '',
// 缓存有效期 0表示永久缓存
'expire' => 0,
// 缓存标签前缀
'tag_prefix' => 'tag:',
// 序列化机制 例如 ['serialize', 'unserialize']
'serialize' => [],
],
// 更多的缓存连接
'redis' => [
'type' => 'redis',
'host' => '127.0.0.1',
'port' => '6379',
'password' => '123456',
'select' => '2'
]
],
];
6.创建接口code码config/code.php
return [
'success' => 100,//成功
'abnormal_error' => -101,//异常错误
'param_error' => -102, //参数验证失败
'phone_error' => -103, //手机号验证失败(已注册或不存在该手机号)
'user_error' => -104, //用户验证失败
'headers_error' => -105, //请求headers错误
'request_error' => -106, //请求方式错误
'param_format_error' => -107, //参数格式校验错误
'param_content_error' => -108, //接口请求参数不能为空
'param_count_error' => -109, //请求参数的个数不正确,请仔细核对
'time_error' => -110, //时间戳有误
'app_key_error' => -111, //app_key无效
'nonce_str_error' => -112, //nonce_str 随机字符串错误
'sign_error' => -113, //sign签名错误
'body_error' => -114, //body不能为空
'timeout_error' => -115, //请求超时
'repeat_error' => -116, //请求重复提交
'supplier_error' => -117, //supplier_id 错误
'supplier_id_error' => -118, //supplier_id 无效
'customer_id_error' => -119, //customer_code无效
'request_num_error' => -120, //请求频繁
'install_repeat' => -121,//重复安装
];
7.去掉访问时域名后面的所带的index.php .htaccess
<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>
location / {
index index.php index.html error/index.html;
if ( !-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
8.接口返回统一格式common.php
```php
/**
* 公共类方法返回通用格式
* @param $status 状态
* @param string $message 提示语
* @param array $data 返回数据
* @param $code 具体状态码
* @return array
*/
function callBack($status, $code, $message = 'ERROR', $data = [])
{
return [
'status' => is_numeric($status) ? $status : config('status.' . $status),
'code' => is_numeric($code) ? $code : config('code.' . $code),
'message' => $message,
'data' => $data
];
}
9.异常处理ExceptionHandle.php
/**
* Render an exception into an HTTP response.
*
* @access public
* @param \think\Request $request
* @param Throwable $e
* @return Response
*/
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
$result = [
'status' => config('status.error'),
'code' => -102,
'message' => $e->getMessage(),
'data' => []
];
if (method_exists($e, 'getStatusCode')) {
$httpStatus = $e->getStatusCode();
} else {
$httpStatus = $this->httpStatus;
}
return json($result, $httpStatus);
}
10.需要继承的控制器
protected $user = null; //用户信息
public function initialize()
{
parent::initialize(); // TODO: Change the autogenerated stub
if (!$this->baseUserInfo()) {
echo json_encode(callBack(config('status.login_overtime'), config('code.user_error'), '登录超时,请重新登录'),JSON_UNESCAPED_UNICODE);
die();
}
}
11.创建异常控制器Error.php
<?php
namespace app\admin\controller;
class Error{
public function __call($method, $args)
{
return json(callBack(config('status.error'),config('code.param_error'),"不存在该{$method}控制器'"));
}
}