路由 use think\Route; //展示添加表单 Route::get(‘create‘,‘user/user/create‘); //表单提交数据 Route::post(‘save‘,‘user/user/save‘); //展示数据 Route::get(‘index‘,‘user/user/index‘); //删除数据 Route::get(‘delete/:id‘,‘user/user/delete‘); //修改展示页面 Route::get(‘edit/:id‘,‘user/user/edit‘); //修改提交数据 Route::post(‘update‘,‘user/user/update‘);
<div class="content"> <div class="header"> <h1 class="page-title">管理员新增页面</h1> </div> <div class="well"> <!-- add form --> <form action="/save" method="post" id="tab" enctype="multipart/form-data"> <label>用户名:</label> <input type="text" name="username" value="" class="input-xlarge"> <label>头像:</label> <input type="file" name="img" value="" class="input-xlarge"> <label>邮箱:</label> <input type="email" name="email" value="" class="input-xlarge"> <label>手机号:</label> <input type="tel" name="tel" value="" class="input-xlarge"> <br> <button class="btn btn-primary" type="submit">保存</button> </form> </div> </div>
<?php namespace app\user\controller; use app\user\model\userModel; use think\Controller; use think\Image; use think\Request; class User extends Controller { /** * 显示资源列表 * * @return \think\Response */ public function index() { //接受关键字 $word = input(‘word‘); $where = [ ‘username‘ => [‘like‘, "%$word%"] ]; $data = userModel::show($where); if (!empty($word)) { foreach ($data as $k => $v) { $v[‘username‘] = str_replace($word, "<font style=‘color:red;‘>$word</font>", $v[‘username‘]); } } //携带参数,去视图 $this->assign(‘data‘, $data); return view(); } /** * 显示创建资源表单页. * * @return \think\Response */ public function create() { // return view(); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function save(Request $request) { // $params = $request->param(); $file = $request->file(‘img‘); //验证图片 if ($file) { $info = $file->move(ROOT_PATH . ‘public‘ . DS . ‘uploads‘); if ($info) { // string(45) "20210820\0fd2d7bea8a34235624d83e914780248.gif" $filename = DS . ‘uploads‘ . DS . $info->getSaveName();// $image = \think\Image::open(‘.‘ . $filename); $image->crop(100, 100)->save(‘.‘ . $filename); //替换 $params[‘img‘] = $filename; } else { // 上传失败获取错误信息 echo $file->getError(); } } $result = userModel::add($params); if (!$result) { $this->error(‘添加失败‘, ‘save‘); } $this->success(‘添加成功‘, ‘/index‘); } /** * 显示指定的资源 * * @param int $id * @return \think\Response */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return \think\Response */ public function edit($id) { // $data = userModel::edit($id); //传输数据至页面 $this->assign(‘data‘, $data); return view(); } /** * 保存更新的资源 * * @param \think\Request $request * @param int $id * @return \think\Response */ public function update(Request $request, $id) { // $params = $request->param(); $imgs = $request->file(‘imgs‘); $params[‘imgs‘] = $imgs; $result = userModel::updateData($params); if (!$result) { $this->error(‘修改失败‘, ‘/index‘); } $this->success(‘修改成功‘, ‘/index‘); } /** * 删除指定资源 * * @param int $id * @return \think\Response */ public function delete($id) { // $result = userModel::del($id); if (!$result) { $this->error(‘删除失败‘, ‘save‘); } $this->success(‘删除成功‘, ‘/index‘); } }
列表展示页面
<div class="content"> <div class="header"> <h1 class="page-title">管理员列表</h1> </div> <div class="well"> <!-- search button --> <form action="index" method="get" class="form-search"> <div class="row-fluid" style="text-align: left;"> <div class="pull-left span4 unstyled"> <p> 用户名:<input class="input-medium" name="word" type="text"></p> </div> </div> <button type="submit" class="btn">查找</button> <a class="btn btn-primary" href="create">新增</a> </form> </div> <div class="well"> <!-- table --> <table class="table table-bordered table-hover table-condensed"> <thead> <tr> <th>编号</th> <th>用户名</th> <th>头像</th> <th>邮箱</th> <th>手机号</th> </tr> </thead> {foreach $data as $k=>$v} <tbody> <tr class="success"> <td>{$k+1}</td> <td>{$v.username}</td> <td><img src="{$v.img}" alt=""></td> <td>{$v.email}</td> <td>{$v.tel}</td> <td> <a href="edit/{$v.id}"> 编辑 </a> <a href="delete/{$v.id}" onclick="return confirm(‘您确定要删除吗?‘)" > 删除 </a> </td> </tr> </tbody> </tbody> {/foreach} </table> <!-- pagination --> </div> {$data->render()} <!-- footer --> <footer> <hr> <p>? 2017 <a href="javascript:void(0);" target="_blank">ADMIN</a></p> </footer> </div>
//修改表单页面
<div class="content"> <div class="header"> <h1 class="page-title">管理员编辑</h1> </div> <div class="well"> <!-- add form --> <form action="/update" method="post" enctype="multipart/form-data"> <label>用户名:</label> <input type="text" name="username" value="{$data.username}" class="input-xlarge"> <label>现头像:</label> <img src="{$data.img}" alt=""> <p><span style="color: green">您可以选择重新上传的照片:</span></p> <input type="file" name="imgs" class="input-xlarge"> <label>邮箱:</label> <input type="email" name="email" value="{$data.email}" class="input-xlarge"> <label>手机号:</label> <input type="tel" name="tel" value="{$data.tel}" class="input-xlarge"> <br> <input type="hidden" name="id" value="{$data.id}"> <button class="btn btn-primary" type="submit">修改</button> </form> </div> <!-- footer --> <footer> <hr> <p>? 2017 <a href="javascript:void(0);" target="_blank">ADMIN</a></p> </footer> </div>
模型页面
<?php namespace app\user\model; use think\Model; class userModel extends Model { // const SUM=2; protected $table=‘user‘; public static function add($params){ return self::create($params,true); } //展示数据 public static function show($where){ return self::where($where) ->paginate( self::SUM); } //删除数据 public static function del($id){ return self::destroy($id); } //修改id展示页面 public static function edit($id){ return self::find($id); } //执行修改 public static function updateData($params){ return self::update($params,$params[‘id‘],true); } }