composer require topthink/think-view 安装视图
$goods_info = $model->lock(true)->where($where)->find(); 悲观锁
模型定义属性值
protected $globalScope = ['is_del'];
public function scopeIs_del($query)
{
$query->where('is_del', 1);
}
pblic function getIsDelAttr($value)
{
$isDel = [1 => '删除', 2 => '禁用', 3 => '正常', 4 => '待审核'];
return $isDel[$value];
}
//post请求验证 只接受postField值 only指定字段
$postField = 'member_id,status';
$data = $this->request->only(explode(',',$postField),'post',null);
$this->validate($post, $rule);
//get common函数 serach_in
data = $this->request->get('status','1','serach_in');
//查询方法过滤
function serach_in($str){
$farr = ["/^select[\s]+|insert[\s]+|and[\s]+|or[\s]+|create[\s]+|update[\s]+|delete[\s]+|alter[\s]+|count[\s]+|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/i"];
$str = preg_replace($farr,'',html_in($str));
return trim($str);
}
/**
* 重写验证规则
* @param array $data
* @param array $validate 验证器名称或者验证规则数组
* @param array $message
* @param bool $batch
* @return bool
*/
public function validate($data, $validate, $message = [], $batch = false, $config = [])
{
try {
parent::validate($data, $validate, $message, $batch);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
return true;
}
自定义模型
class B2bAppsAutoModel extends TimeModel
{
protected $table ='';
public static $TableNameModel;
public function __construct($data=[])
{
parent::__construct($data);
if(is_null(static::$TableNameModel)){
throw new ValidateException('必须指定 TableNameModel');
}
$this->table = static::$TableNameModel;
}
}
//模型值替换
public function getTypeIdAttr($value)
{
$status = [1=>'爱读宝横屏',2=>'爱读宝H5端',3=>'爱读宝PC端',4=>'爱读宝竖屏'];
return ['val'=>$value,'text'=>$status[$value]];
}
//验证码
if (!Captcha::check($data['captcha'])) {
// 验证失败
echo '失败';
}else{
echo '成功';
}
middleware.php 文件\think\middleware\SessionInit::class开启
//验证码
if (!Captcha::check($data['captcha'])) {
// 验证失败
echo '失败';
}else{
echo '成功';
}
middleware.php 文件\think\middleware\SessionInit::class开启
//中间件使用
1:模块下定义中间件
middleware下创建中间件
public function handle($request, \Closure $next)
{
if ($request->param('name') == '111') {
//必须对象方法输出
$request->hello =$request->param('name');
return $next($request);
}
$request->hello = 'ThinkPHP';
return $next($request);
}
2:config模块下注册中间件
return ['alias' => [ '名称'=>路径::class,]]
3:控制器调用中间件
protected $middleware = ['名称'];
4:$request->xxx 必须对象调用
/判断是否存在某张表 得到存在的表
db()->query('SHOW TABLES LIKE ' . "'" . "b2b_apps_book_resource_%" . "'");