THINK PHP5 学习
程序员文章站
2022-04-25 20:28:13
...
两周没有写博客了,这两周主要是在学习thinkPHP5,主要以看视频为主的学习,今天就是对thinkPHP5做一定的总结;
总体来说thinkPHP学习上还是比较顺利;
学习手册--https://www.kancloud.cn/manual/thinkphp5/
thinkPHP5快速手册以及完整手册;
用Composer下载目录:
对应项目目录
控制器的写法:
<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Loader;
use app\admin\model\Admin as AdminModel;
class Admin extends Controller
{
public function lst()
{
$list = AdminModel::paginate(3);
$this->assign('list',$list);
return $this->fetch();
}
public function add()
{
if(request()->isPost()){
$data = [
'username'=>input('username'),
'password'=>md5(input('password')),
];
$validate =Loader::validate('Admin');
if(!$validate->scene('add')->check($data)){
$this->error($validate->getError());
}
if(Db::name('admin')->insert($data)){
return $this->success('添加管理者成功','lst');
}else{
return $this->error('添加管理员失败');
}
}
return $this->fetch();
}
public function edit()
{
$id = input('id');
$admins = db('admin')->find($id);
$this->assign("admins",$admins);
if(request()->isPost()){
$data = [];
if(input('password')===""){
$data = [
'id'=>$id,
'username'=>input('username'),
'password'=>$admins['password'],
];
}else{
$data = [
'id'=>$id,
'username'=>input('username'),
'password'=>md5(input('password')),
];
}
$validate =Loader::validate('Admin');
if(!$validate->scene('add')->check($data)){
$this->error($validate->getError());
}
if(Db::name('admin')->update($data)){
return $this->success('编辑管理者成功','lst');
}else{
return $this->error('编辑管理员失败');
}
}
return $this->fetch();
}
public function del()
{
$id = input('id');
if($id!=1){
if(Db::name('admin')->delete($id)){
return $this->success("删除成功",'lst');
}else{
return $this->error("删除管理员失败!",'lst');
}
}else{
return $this->error("系统管理员不能删除",'lst');
}
}
public function logout(){
session("username",'');
session("uid","");
return $this->fetch('login/index');
}
}
视图层代码:
<?php
namespace app\admin\model;
use think\Model;
use think\Db;
use think\Session;
class Admin extends Model{
public function login($data){
$user = Db::name('admin')->where('username','=',$data['username'])->find();
if($user){
if($user['password']==md5($data['password'])){
session("username",$user['username']);
session("uid",$user['id']);
return 3; //信息正確
}else{
return 2; // 密碼錯誤
}
}else{
return 1; // 用戶不存在
}
}
}
校验过滤:
<?php
namespace app\admin\validate;
use think\Controller;
use think\Validate;
class Admin extends Validate{
protected $rule = [
'username' =>'require|min:5|unique:admin',
'password' =>'require|min:6',
'id' => 'require',
];
protected $message = [
'username.require' => '管理员名称必须填写',
'password.require' => '管理员密码不能为空',
'username.min' => '管理员名称不能小于6位',
'username.unique' => '管理员名称不得重复',
'password.min' => '管理员密码不能小于6位',
'id.require' => '管理员密码不能小于6位',
];
protected $scene = [
'add' => ['username','password'],
'edit' => ['id','username'],
];
}
视图页面代码:
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<title>童老师ThinkPHP交流群:484519446</title>
<meta name="description" content="Dashboard">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--Basic Styles-->
<link href="__PUBLIC__/style/bootstrap.css" rel="stylesheet">
<link href="__PUBLIC__/style/font-awesome.css" rel="stylesheet">
<link href="__PUBLIC__/style/weather-icons.css" rel="stylesheet">
<!--Beyond styles-->
<link id="beyond-link" href="__PUBLIC__/style/beyond.css" rel="stylesheet" type="text/css">
<link href="__PUBLIC__/style/demo.css" rel="stylesheet">
<link href="__PUBLIC__/style/typicons.css" rel="stylesheet">
<link href="__PUBLIC__/style/animate.css" rel="stylesheet">
</head>
<body>
<!-- 头部 -->
{include file="common/top"}
<div class="main-container container-fluid">
<div class="page-container">
{include file="common/left"}
<!-- /Page Sidebar -->
<!-- Page Content -->
<div class="page-content">
<!-- Page Breadcrumb -->
<div class="page-breadcrumbs">
<ul class="breadcrumb">
<li>
<a href="#">系统</a>
</li>
<li class="active">管理员管理</li>
</ul>
</div>
<!-- /Page Breadcrumb -->
<!-- Page Body -->
<div class="page-body">
<button type="button" tooltip="添加管理员" class="btn btn-sm btn-azure btn-addon" onClick="javascript:window.location.href = '{:url('admin/admin/add')}'"> <i class="fa fa-plus"></i> Add
</button>
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-body">
<div class="flip-scroll">
<table class="table table-bordered table-hover">
<thead class="">
<tr>
<th class="text-center">ID</th>
<th class="text-center">管理员名称</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
{volist name="list" id="user"}
<tr>
<td align="center">{$user.id}</td>
<td align="center">{$user.username}</td>
<td align="center">
<a href="{:url('admin/edit',array('id'=>$user['id']))}" class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 编辑
</a>
<a onClick="warning('确实要删除吗','{:url('admin/del',array('id'=>$user['id']))}')" class="btn btn-danger btn-sm shiny">
<i class="fa fa-trash-o"></i> 删除
</a>
</td>
</tr>
{/volist}
</tbody>
</table>
{$list->render()}
</div>
<div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /Page Body -->
</div>
<!-- /Page Content -->
</div>
</div>
<!--Basic Scripts-->
<script src="__PUBLIC__/style/jquery_002.js"></script>
<script src="__PUBLIC__/style/bootstrap.js"></script>
<script src="__PUBLIC__/style/jquery.js"></script>
<!--Beyond Scripts-->
<script src="__PUBLIC__/style/beyond.js"></script>
</body></html>
多敲多练 多总结!
加油!