thinkphp小知识
tp6定义全局常量位置:confing/view.php
跨控制器调用
thinkphp5
protected $service;
public function __construct(CenterOutService $service)
{
parent::__construct();
$this->service = $service;
}
public function index()
{
$this->assign(['list' => $this->service->page()]);
return view();
}
thinkphp6
$right=new \app\controller\Right($this->app);
模板页获取请求参数
tp6 $request= \think\facade\Request::instance();
tp5 $request= \think\Request::instance();
选项卡添加样式
<script>
var className = '{$request->action()}_{$request->param("id")}'
//$("."+className).addClass('active');
$("."+className).prepend('<span/>');
</script>
<script>
var className = '{$request->controller()}-{$request->action()}'
$("."+className).addClass('active');
$("."+className).parent().parent().addClass('active open');
$("."+className).parent().show();
</script>
封装where查询条件
public function page(){
$data = Request::instance()->get();
$where = [];
//封装where查询条件
(empty($data['status']) || $data['status'] == '') || $where['status'] = $data['status'];
empty($data['name']) || $where['name'] = ['like','%'.$data['name'] ];
empty($data['out_pack']) || $where['out_pack | in_pack'] = ['like','%'.$data['out_pack'].'%' ];
return Company::where($where)->paginate(10);
}
验证码
<input name="captcha" type="text" maxlength="8" autocomplete="off" class="form-control" errormsg="请输入验证码!" datatype="*4-6" nullmsg="请输入验证码!">
<img title="点击刷新" src="{:captcha_src()}" style="cursor:pointer"
onclick="this.src='{:captcha_src()}?id='+Math.random()+''">
$captcha = $this->request->post('captcha');
if (!captcha_check($captcha)) {
$this->error("验证码不正确");
}else{
$this->success("正确");
}
thinkcmf 自动路由
<td>
<notempty name="vo.categories">
<a href="{:cmf_url('portal/Article/index',array('id'=>$vo['id'],'cid'=>$vo['categories'][0]['id']))}"
target="_blank">{$vo.post_title}666</a>
<else/>
<a href="{:cmf_url('portal/Article/index',array('id'=>$vo.id))}"
target="_blank">{$vo.post_title}</a>
</notempty>
</td>
<td>
<foreach name="vo.categories" item="voo">
<a class="label label-default"
href="{:cmf_url('portal/List/index',array('id'=>$voo['id']))}"
target="_blank"
>{$voo.name}</a>
</foreach>
</td>
模板标签
{$vo.post_content | htmlspecialchars_decode |raw |strip_tags| mb_substr=0,62}...
{$vo.pick_status==0?'正常':'废弃'}
{:date('Y-m-d H:i',$user->add_time)}
{$content->create_time|date="Y-m-d H:i:s",###}