tp5是什么?全民为ThinkPHP
ThinkPHP是一个免费开源的,快速、简单的面向对象的轻量级PHP开发框架,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。遵循Apache2开源许可协议发布,意味着你可以免费使用ThinkPHP,甚至允许把你基于ThinkPHP开发的应用开源或商业产品发布/销售。
在Controller控制器下新建
Common.php
<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;
class Common extends Controller
{
//检查是否登录
public function _initialize()
{
if(!session('uname')){
$this->error('请先登录!',url('/admin/login/index'));
}
}
}
在需要判断登录的类中继承该类:Index.php
<?php
namespace app\admin\controller;
/**
* Created by PhpStorm.
* User: gao
* Date: 2019/6/11
* Time: 15:39
*/
use think\Controller;
use think\Db;
class Index extends Common
{
//框架应用
public function Index(){
return view("index");
}
/**
* 文章内容显示
*/
public function AList(){
$sou = request()->param();
$name = input('tid') ? input('title') : ($sou['title'] = '');
//判断是不是第一栏目
$t_fid = input('t_fid') ? input('t_fid') : 0;
$Z_tid = 2;
if(isset($_POST['sousuo'])){
$tid = input("tid");
$title = input("title");
$data = array();
if(!empty($tid)){
$data['a.tid'] = $tid;
}
if(!empty($title)){
$data['title'] = ['like','%'.$title.'%'];
}
$arr=Db::table('article')
->alias('a')
->join('type t','a.tid=t.tid')
->field('a.*,t.tname')//查询指定字段
->where('a.tid','neq',$Z_tid)
->where($data)
->group('a.id')//分组
->paginate(5, false, ['query' => request()->param()]);//分页
$page=$arr->render();
$tid = Db::name('type')->select();
return view("list",['arr'=>$arr,'tid'=>$tid,'page'=>$page,'title'=>$title]);
}else{
$arr=Db::table('article')
->alias('a')
->join('type t','a.tid=t.tid')
->field('a.*,t.tname')//查询指定字段
->where('a.tid','neq',$Z_tid)
->order("id desc")
->group('a.id')//分组
->paginate(5, false, ['query' => request()->param()]);//分页
$page=$arr->render();
$tid = Db::name('type') ->where('tid','neq',$Z_tid)->select();
return view("list",['arr'=>$arr,'tid'=>$tid,'page'=>$page,'title'=>""]);
}
}
/**
* 文章类型删除
*/
public function type_delete(){
$id=$_GET['id'];
$rs=Db::table('type')->where('tid',$id)->delete();
if($rs)
{ $this->success('删除成功','admin/Index/typeContent');}
else
{ $this->error('删除失败','admin/Index/typeContent');}
}
/**
* 文章类型修改
*/
public function type_update()
{
$id = input('tid');
if (isset($_POST['sub'])) {
$tname = input("tname");
$xiugai = Db::name('type')
->where('tid', $id)
->update(['tname' => $tname]);
if ($xiugai)
$this->success('修改成功', 'admin/Index/typeContent');
else
$this->error('修改失败', 'admin/Index/typeContent');
}else{
$id = input('id');
$array = Db::name('type')->where('tid',$id)->find();
return view("type_update",['arr'=>$array]);
}
}
}
在不需要判断是否登录的类中不继承该类,继承Controller就行:Login.php
<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
class Login extends Controller{
public function index(){
if(null!=input('submit'))
{
$name=input('name');
$pwd=input('pass');
$user=Db::table('admin')
->where('uname',$name)
->where('upwd',$pwd)
->find();
if($user)
{
Session::set('uname',$name);
$this->success('登陆成功','admin/Index/index');
}
else
{
$this->error('用户名或密码错误','index');
}
}else{
return view('login');
}
}
function dieLogin()
{
Session(null);
return view('login');
}
}