abstrak:<?php /** * Created by PhpStorm. * User: 葉子 * Date: 2019/3/2 * Time: 18:33 */ namespace app\admin\controller; sessio
<?php /** * Created by PhpStorm. * User: 葉子 * Date: 2019/3/2 * Time: 18:33 */ namespace app\admin\controller; session_start(); use app\model\User; use pig\core\Controller; class Index extends Controller { public function __construct() { parent::__construct(); } // test public function test($name = 'leaf', $age = 12) { return 'I am ' . $name . ' now ' . $age; } // testview 演示plates模板引擎的基本使用 public function testView() { // 變量 $site = '神奇寶貝'; // 數(shù)組 $course = ['皮卡丘', '波加曼', '天蝎王']; // 對象 $zhi = new \stdClass(); $zhi->name = '小智'; $zhi->age = 10; $zhi->course = '神奇寶貝大師'; // 模板賦值 $this->assign('site', $site); $this-> // 模板渲染 $this->fetch(__DIR__ . '/../view/index/testview.html'); } // 用戶信息列表 public function index() { $rows = (new User())->select('user', ['id', 'name', 'email', 'dept', 'art', 'create_time'],[ // 搜索功能,如果用戶提交了搜索條件,則根據(jù)條件搜索,否則顯示全部 'dept[~]' => isset($_POST['dept']) ? $_POST['dept'] :null ]); // 渲染模板 return $this->view->render('admin::index/index', [ 'rows' => $rows, 'title' => '武榜', 'loginUrl' => '/admin/index/login', // 登錄 'logoutUrl' => '/admin/index/logout', // 登出 'indexUrl' => '/admin/index/index', // 返回首頁 'insUrl' => 'admin/index/insert', // 添加操作 'editUrl' => 'admin/index/edit', // 編輯操作 'delUrl' => 'admin/index/delete' // 刪除操作 ]); } // 管理員登錄 public function login() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 驗證用戶 $res = (new User())->get('admin', ['name', 'email', 'password'], [ 'AND' => [ 'email' => $_POST['email'], 'password' => sha1($_POST['password']), ] ]); if ($res == false) { echo "<script>alert('用戶名或密碼錯誤');location.href='/';</script>"; } else { $_SESSION['name'] = $res['name']; echo "<script>alert('登陸成功');location.href='/';</script>"; } } } // 退出登錄 public function logout() { session_destroy(); echo "<script>alert('退出成功');location.href='/';</script>"; } // 添加數(shù)據(jù) 1. 渲染一個添加數(shù)據(jù)的表單 2. 添加操作 public function insert() { return $this->view->render('admin::index/insert', [ 'title' => '添加數(shù)據(jù)', 'url' => '/admin/index/add', ]); } // 添加操作 public function add() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 執(zhí)行添加操作 (new User())->insert('user', [ 'name' => $_POST['name'], 'dept' => $_POST['dept'], 'art' => $_POST['art'], 'email' => $_POST['email'], 'create_time' => time(), ]); echo "<script>alert('添加成功');location.href='/';</script>"; } } // 編輯操作 // 渲染編輯模板 public function edit($id = '') { $row = (new User())->get('user', ['id', 'name', 'email', 'dept', 'art'], ['id' => $id]); return $this->view->render('admin::index/edit',[ 'title' => '編輯用戶', 'url' => '/admin/index/save', 'row' => $row, ]); } public function save($id) { if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 執(zhí)行編輯操作 (new User())->update('user', [ 'name' => $_POST['name'], 'dept' => $_POST['dept'], 'art' => $_POST['art'], 'email' => $_POST['email'], ],['id'=>$id]); echo "<script>alert('編輯成功');location.href='/';</script>"; } } // 刪除操作 public function delete($id) { (new User())->delete('user', ['id' => $id]); echo "<script>alert('刪除成功');location.href='/';</script>"; } }
Guru membetulkan:韋小寶Masa pembetulan:2019-03-06 09:12:35
Rumusan guru:寫的還是很不錯的 整體的輸入框還是可以改進(jìn)改進(jìn)的哦 其他的邏輯什么的都沒任何毛病