杂项
缓存
ThinkPHP采用think\Cache
类(实际使用think\facade\Cache
类即可)提供缓存功能支持。
内置支持的缓存类型包括file、memcache、wincache、sqlite、redis。
config\cache.php
return [
'default' => 'file',
'stores' => [
// 文件缓存
'file' => [
// 驱动方式
'type' => 'file',
// 设置不同的缓存保存目录
'path' => '../runtime/file/',
],
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => '127.0.0.1',
],
],
];
用法示例
// 设置缓存有效期 缓存在3600秒之后过期
Cache::set('name', $value, 3600);
// 缓存自增 针对数值类型的缓存数据
Cache::set('name', 1);
// name自增(步进值为1)
Cache::inc('name');
// name自增(步进值为3)
Cache::inc('name',3);
// 缓存自减 针对数值类型的缓存数据
// name自减(步进值为1)
Cache::dec('name');
// name自减(步进值为3)
Cache::dec('name',3);
// 获取缓存并且设置默认值
Cache::get('name','');
// 追加一个缓存数据 如果是数组
Cache::set('name', [1,2,3]);
Cache::push('name', 4);
Cache::get('name'); // [1,2,3,4]
// 删除缓存
Cache::delete('name');
// 获取并删除缓存
Cache::pull('name');
// 清空缓存
Cache::clear();
// 助手函数
// 设置缓存数据
cache('name', $value, 3600);
// 获取缓存数据
var_dump(cache('name'));
// 删除缓存数据
cache('name', NULL);
// 返回缓存对象实例
$cache = cache();
Session
可以直接使用think\facade\Session
类操作Session
开启Session
app\middleware.php
<?php
// 全局中间件定义文件
return [
// 全局请求缓存
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
\think\middleware\SessionInit::class
];
用法示例
// 设置Session
Session::set('name', 'thinkphp');
// 判断是否存在
Session::has('name');
// 取值
// 如果值不存在,返回null
Session::get('name');
// 如果值不存在,返回空字符串
Session::get('name', '');
// 获取全部数据
Session::all();
// 删除
Session::delete('name');
// 取值并删除
Session::pull('name');
// 清空
Session::clear();
// 设置session 并且在下一次请求之前有效
Session::flash('name','value');
// 清除当前请求有效的session
Session::flush();
// 支持session的多级数组操作,其中set和delete方法只能支持二级数组,其他方法支持任意级数组操作。例如:
// 赋值
Session::set('name.item','thinkphp');
// 判断是否赋值
Session::has('name.item');
// 取值
Session::get('name.item');
// 删除
Session::delete('name.item');
// 助手函数
// 赋值
session('name', 'thinkphp');
// 判断是否赋值
session('?name');
// 取值
session('name');
// 删除
session('name', null);
// 清除session
session(null);
Cookie
ThinkPHP采用think\facade\Cookie类提供Cookie支持
config\cookie.php
<?php
// +----------------------------------------------------------------------
// | Cookie设置
// +----------------------------------------------------------------------
return [
// cookie 保存时间
'expire' => 0,
// cookie 保存路径
'path' => '/',
// cookie 有效域名
'domain' => '',
// cookie 启用安全传输
'secure' => false,
// httponly设置
'httponly' => false,
// 是否使用 setcookie
'setcookie' => true,
// samesite 设置,支持 'strict' 'lax'
'samesite' => '',
];
用法示例
// 设置Cookie 有效期为 3600秒
Cookie::set('name', 'value', 3600);
// 永久保存Cookie
Cookie::forever('name', 'value');
//删除cookie
Cookie::delete('name');
// 读取某个cookie数据
Cookie::get('name');
// 获取全部cookie数据
Cookie::get();
// 助手函数
// 设置
cookie('name', 'value', 3600);
// 获取
echo cookie('name');
// 删除
cookie('name', null);
上传
配置磁盘
config\filesystem.php
<?php
return [
// 默认磁盘
'default' => env('filesystem.driver', 'local'),
// 磁盘列表
'disks' => [
'local' => [
'type' => 'local',
'root' => app()->getRuntimePath() . 'storage',
],
'public' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
'root' => app()->getRootPath() . 'public/storage',
// 磁盘路径对应的外部URL路径
'url' => '/storage',
// 可见性
'visibility' => 'public',
],
// 新增用于上传图片
'uploads' => [
// 磁盘类型
'type' => 'local',
// 磁盘路径
'root' => app()->getRootPath() . 'public/',
// 磁盘路径对应的外部URL路径
'url' => '/uploads',
// 可见性
'visibility' => 'public',
],
// 更多的磁盘配置信息
],
];
单图上传
if(!function_exists('upload'))
{
/**
* 单图上传
* @param String $name 字段名
* @return Array 文件上传结果数组
*/
function upload($name)
{
try{
$file = request()->file($name);
if($file)
{
// 保存指定目录
$FileName = \think\facade\Filesystem::disk('uploads')->putFile('uploads',$file);
if($FileName)
{
// 把\替换/
$FileName = str_replace('\\','/',$FileName);
// 返回数组
return ['code' => 1,'msg' => '上传成功','data' => '/' . $FileName];
}
}
}catch(\Exception $e)
{
return ['code' => 0,'msg' => $e->getMessage(),'data' => []];
}
}
}
多图上传
if(!function_exists('uploads'))
{
/**
* 多图上传
* @param String $name 字段名
* @return Array 上传文件结果数组
*/
function uploads($name)
{
try{
// 接收到所有的图片
$files = request()->file($name);
// 定义一个空的数组
$List = [];
if($files)
{
foreach ($files as $item) {
// 保存指定目录
$FileName = \think\facade\Filesystem::disk('uploads')->putFile('uploads',$item);
// 把\替换/
$FileName = str_replace('\\','/',$FileName);
$List[] = '/' . $FileName;
}
if(count($List) > 0)
{
// 返回结果
return ['code' => 1,'msg' => '上传成功','data' => $List];
}
}
}catch(\Exception $e)
{
return ['code' => 0,'msg' => $e->getMessage(),'data' => []];
}
}
}
验证码
安装
composer require topthink/think-captcha
使用
<div>{:captcha_img()}</div>
或者
<div><img src="{:captcha_src()}" alt="captcha" /></div>
验证
使用框架的内置验证功能
$this->validate($data,[
'captcha|验证码'=>'require|captcha'
]);
如果没有使用内置验证功能,则可以调研内置的函数手动验证
if(!captcha_check($captcha)){
// 验证失败
};
配置
参数 | 描述 | 默认 |
---|---|---|
codeSet | 验证码字符集合 | 略 |
expire | 验证码过期时间(s) | 1800 |
math | 使用算术验证码 | false |
useZh | 使用中文验证码 | false |
zhSet | 中文验证码字符串 | 略 |
useImgBg | 使用背景图片 | false |
fontSize | 验证码字体大小(px) | 25 |
useCurve | 是否画混淆曲线 | true |
useNoise | 是否添加杂点 | true |
imageH | 验证码图片高度,设置为0为自动计算 | 0 |
imageW | 验证码图片宽度,设置为0为自动计算 | 0 |
length | 验证码位数 | 5 |
fontttf | 验证码字体,不设置是随机获取 | 空 |
bg | 背景颜色 | [243, 251, 254] |
reset | 验证成功后是否重置 | true |
用法示例
app\index\config\captcha.php
return [
'length' => 4,
'codeSet' => '0123456789',
];
适用于thinkphp6.0的跳转扩展
安装
composer require liliuwei/thinkphp-jump
配置
config\jump.php
<?php
return[
// 默认跳转页面对应的模板文件
'dispatch_success_tmpl' => app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl',
'dispatch_error_tmpl' => app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl',
];
用法示例
所需控制器内引用该扩展
<?php
namespace app\controller;
class Index
{
use \liliuwei\think\Jump;
public function index()
{
//return $this->error('error');
//return $this->success('success','index/index');
//return $this->redirect('/admin/index/index');
return $this->result(['username' => 'liliuwei', 'sex' => '男']);
}
}
在框架自带的BaseController里引入,以后所有需要使用跳转的类继承自带的基类控制器
<?php
declare (strict_types = 1);
namespace app;
use think\App;
use think\exception\ValidateException;
use think\Validate;
/**
* 控制器基础类
*/
abstract class BaseController
{
use \liliuwei\think\Jump;
......
}
继承BaseController后即可使用success、error、redirect、result方法