TP控制器调用代码块
namespace app\index\controller;
use think\facade\View;
use app\BaseController;
use think\facade\Session;
use think\facade\Db;
use think\facade\Route as Url;
use app\index\model\User as Auth;
use think\facade\Request;
use think\helper\Str;
class Company extends BaseController{
if(!Auth::hasLogin()) return $this->redirect(Url::buildUrl('user/login')->suffix(false)->build());
$where=[ 'status' => $status];
if($status==0){$where=1;}
$uid = Session::get('uid');
$userinfo = Auth::with('cominfo')->find($uid)->toArray();
$prolist=Db::table('project')->where($where)->select();
View::assign([
'userinfo' => $userinfo,
]);
return View::fetch();
返回部分字段
Db::table('think_user')->where('status',1)->column('name', 'id');
Db::table('user')->field('id,title,content')->select();
对数据重组
$orderarr=$orderlist->toArray();
foreach($list as &$v){}
if (!request()->isPost()) return $this->failed('请登陆!');
$ajax=Request::param();
$images=array('pic1'=>$ajax['biji_pic1'],'pic2'=>$ajax['biji_pic2'],'pic3'=>$ajax['biji_pic3']);
$proinfo=array();
$isok=Db::connect('demo')->Db::name('project')->insert($proinfo);
$DTb ->where('pid', $pid) ->inc('click')->update();
模板调用常用变量格式
{$Think.config.app.cdn}
{volist name="prolist" id="vo"}
{$vo.title}
{:date("Y-m-d H:i",$vo.endtime)}
{:url('news/detail', ['newid'=>$vo.newid])}
{/volist}
{$Think.config.app.cdn}
{$info|raw}//防止转义
{eq name="status" value="0"}class="default"{/eq}
{gt name="vo.winnum" value="0"}{$vo.pronum} {else/} {$vo.winnum}{/gt}
{$detail.title|default=""}
{switch vo.status }
{case 1}待付保证金{/case}
{case 2}待审核{/case}
{default /}已完成
{/switch}
查询
$map=array();
if(input('param.ccate') != ''){$map[] =['ccate','=',input('param.ccate')];}
if(input('param.title') != ''){$map[] = ['title','like','%'.input('param.title').'%'];}
$map['pic'] = array('exp','is not null');
分页
$where=['ischeck'=>1,'cid'=> $cid];
if($cid==0){$where=['ischeck'=>1];}
$caselist = Db::name('cases')->where($where)->order('updatetime', 'desc')->paginate(12);
$page = $caselist->render();
{$page|raw}
$proinfo['description']=Str::substr(strip_tags($ajax['content']), 0,150);
修改栏目样式使用{eq name="Request.controller" value="company.User"}{eq name="Request.action" value="index"}class="default"{/eq}{/eq}
调试变量
dump($var1, ...$varN),可一次操作多个变量 ,替代print_r halt($blog, $user);类似于die();
{include file="header" /}
修改分页,需要先改provider配置
tp3获取控制器名称{$Think.CONTROLLER_NAME}
tp3获取操作名称{$Think.ACTION_NAME}
foreach($list as &$v){
$v['drawtime']=date("Y-m-d H:i", $v['drawtime']);
$v['publishtime']=date("Y-m-d H:i", $v['publishtime']);
$v['laiyuan']=laiyuan($v['fid']);
$product['thumb']=str_replace('https://ayougold.com/uploads/','',$v['thumb'])
}
新安装的thinkphp6 开启了调试模式,get接口中会出现trace输出的信息,导致接口中返回的数据无法解析。
在 think\trace\Service 中将 $this->app->middleware->add(TraceDebug::class); 注释掉即可
开启时在 middleware.php 中添加 \think\trace\TraceDebug::class即可
$map['hot']=[ 1,2]; 条件判断部分请参照weixin的news控制器
$map['id'] = array('eq',100);
$map['id'] = array('neq',100);
$map['id'] = array('gt',100);
$map['id'] = array('egt',100);
$map['name'] = array('like','thinkphp%');
$map['a'] =array('like',array('%thinkphp%','%tp'),'OR');
$map['b'] =array('notlike',array('%thinkphp%','%tp'),'AND');
$map['id'] = array('between','1,8');
$map['id'] = array('between',array('1','8'));
$map['id'] = array('not in','1,5,8');
$map['id'] = array('not in',array('1','5','8'));
$map['id'] = array('in','1,3,8');
$map['id'] = array('exp',' IN (1,3,8) ');
$where[] = ['e.create_time', 'EXP', Db::raw('IS NOT NULL')];
调试
use think\facade\Log;
Log::write('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'],'notice');
Log::write($orderid,'notice');
sqlite
'talk' => [
'type' => 'sqlite',
'hostname' => '127.0.0.1',
'database' => '/data/wwwroot/talk/public/dianhua2.db',
'charset' => 'utf8',
],
$list=Db::connect('talk')->table('dh_answer')->select();
绑定 模块及控制器
$_GET['s']="tool/test";
$response = $http->name('admin')->run();
// 调用Request对象的param方法 传入参数为name
{$Request.param.name}
// 调用Request对象的param方法 传入参数为user.nickname
{$Request.param.user.nickname}
// 调用Request对象的root方法
{$Request.root}
// 调用Request对象的root方法,并且传入参数true
{$Request.root.true}
// 调用Request对象的path方法
{$Request.path}
// 调用Request对象的module方法
{$Request.module}
// 调用Request对象的controller方法
{$Request.controller}
// 调用Request对象的action方法
{$Request.action}
// 调用Request对象的ext方法
{$Request.ext}
// 调用Request对象的host方法
{$Request.host}
// 调用Request对象的ip方法
{$Request.ip}
// 调用Request对象的header方法
{$Request.header.accept-encoding}
$where_1['a'] = 1;
$where_1['b'] = 2;
$where_2['c'] = 3;
$where_2['d'] = 4;
$where_main['_complex'] = array(
$where_1,
$where_2,
'_logic' => 'or'
);
$where_main['e'] = 5;
$this->user->where($where_main)->select();
IP地址:
{$Request.ip}
tp版本:
{:think\\App::VERSION}
控制器名称:
{$Request.controller}
session变量:
{$Request.session.mch_id}
GET/POST传参:
{$Request.param.cw_id}
控制器里,方法名称:
{$Request.action}
域名,主机名:
{$Request.host}
URL参数
{$_SERVER['REQUEST_URI']}
$a=" ";
if(empty($a)){return 结果;}//如果两个引号之间有空格则返回错,如果引号之间没有空格则返回对,另外null 0,或者数组空键值都返回对,返回对吉empty($a)为真