菜单的编辑删除功能控制器

Original 2019-04-29 14:24:48 322
abstract:


namespace app\admin\controller;

use think\Controller;

class Admin extends Controller

{

public function lists()

{

$adminres = \think\Db::name('admin')->paginate(3);

$this->assign('adminres',$adminres);

return $this->fetch();

}

public function add()

{

if(request()->isPost()){

$data = [

'username'=>input('username'),

'password'=>input('password'),

];

$validate = \think\Loader::validate('Admin');

if($validate->check($data)){

$data['password'] = md5($data['password']);

$res = \think\Db::name('admin')->insert($data);

if($res){

return $this->success('管理员成功','lists');

}else{

return $this->error('管理员失败');

}

}else{

//验证失败输出提示信息

return $this->error($validate->getError());

}

return;

}

return $this->fetch();

}

public function del()

{

$id=input('id');

if($id==1){

return $this->error('初始管理员不能删除!');

}else{

if(db('admin')->delete($id)){

return $this->success('删除管理员成功','lists');

}else{

return $this->error('删除管理员失败');

}

}

}

public function edit()

{

//获取修改的信息

$id=input('id');

$admins = db('admin')->find($id);

$this->assign('admins',$admins);//模板赋值

//修改后提交

if(request()->isPost()){

$data=[

'id'=>input('id'),

'username'=>input('username'),

'password'=>input('password'),

];

$validate = \think\Loader::validate('Admin');

if($validate->check($data)){

$data['password'] = md5($data['password']);

$res = \think\Db::name('admin')->update($data);

if($res){

return $this->success('修改管理员成功','lists');

}else{

return $this->error('修改管理员失败');

}

}else{

return $this->error($validate->getError());

}

}

return $this->fetch();

}

}



Correcting teacher:查无此人Correction time:2019-04-30 09:03:32
Teacher's summary:完成的不错,你这个是使用tp框架写的,可以先封装下。继续加油

Release Notes

Popular Entries