1、入口文件index.php
<?php
function dd($params)
{
echo "<pre>";
var_dump($params);
echo "</pre>";
die;
}
function responseSuccess($data, $message = '')
{
header('Content-type: application/json');
echo json_encode([
'status' => 'ok',
'message' => $message,
'data' => $data
]);
exit();
}
function responseError($message, $data = '')
{
header('Content-type: application/json');
echo json_encode([
'status' => 'error',
'message' => $message,
'data' => $data
]);
exit();
}
define('ROOT_DIR', dirname(__FILE__));//结果是demo_05
// index.php?controller=demo&action=show
$controller = isset($_GET['controller']) ? $_GET['controller'] : ''; // demo
$action = isset($_GET['action']) ? $_GET['action'] : ''; // show
if (!$controller) {
dd('controller不可为空');
}
if (!$action) {
dd('action不可为空');
}
$className = sprintf('%sController', $controller);
$action = sprintf('%sAction', $action);
$controllerPath = sprintf('%s/controller/%s.php', ROOT_DIR, $className);//文件的绝对路径
if (!file_exists($controllerPath)) {
dd('文件不存在');
}
require_once $controllerPath;//引入demoController.php文件
if (!class_exists($className)) {
dd('类不存在');
}
$class = new $className();
if (!method_exists($class, $action)) {
dd('方法不存在');
}
$class->$action();
2.demoController.php
<?php
require_once ROOT_DIR . '/model/demoModel.php';
/**
* demoController
*/
class demoController
{
public function showAction()
{
require_once ROOT_DIR . '/view/demo.html';
}
public function getNameAction()
{
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$name = '';
if ($id > 0) {
$model = new demoModel();
$name = $model->getName($id);
}
header('Content-type: application/json');
echo json_encode([
'status' => 'ok',
'message' => '',
'data' => [
'name' => $name
]
]);
}
public function listAction()
{
$model = new demoModel();
$list = $model->getList();
require_once ROOT_DIR . '/view/list.html';
}
public function editAction()
{
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id < 1) {
echo 'ID不可小于1';
exit();
}
$model = new demoModel();
$user = $model->getUser($id);
if (!$user) {
echo '用户不存在';
exit();
}
require_once ROOT_DIR . '/view/edit.html';
}
public function doDeleteAction()
{
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
if ($id < 1) {
responseError('ID不可小于1');
}
$model = new demoModel();
$result = $model->remove($id);
if ($result) {
responseSuccess([]);
}
responseError('删除失败');
}
//用于确定是否修改
public function doEditAction()
{
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
$name = isset($_POST['name']) ? (string)$_POST['name'] : '';
$sex = isset($_POST['sex']) ? (string)$_POST['sex'] : 'male';
$age = isset($_POST['age']) ? (int)$_POST['age'] : 0;
$description = isset($_POST['description']) ? (string)$_POST['description'] : '';
if ($id < 1) {
responseError('ID不可小于1');
}
if (!$name) {
responseError('姓名不可为空');
}
if (!in_array($sex, ['male', 'female'])) {
responseError('性别不存在');
}
if ($age < 1) {
responseError('年龄不可小于1');
}
if (!$description) {
responseError('描述不可为空');
}
$model = new demoModel();
$result = $model->update($id, [
'name' => $name,
'sex' => $sex,
'age' => $age,
'description' => $description
]);
if ($result) {
responseSuccess([]);
}
responseError('更新失败');
}
public function addAction()
{
require_once ROOT_DIR . '/view/add.html';
}
public function doAddAction()
{
$name = isset($_POST['name']) ? (string)$_POST['name'] : '';
$sex = isset($_POST['sex']) ? (string)$_POST['sex'] : 'male';
$age = isset($_POST['age']) ? (int)$_POST['age'] : 0;
$description = isset($_POST['description']) ? (string)$_POST['description'] : '';
if (!$name) {
responseError('姓名不可为空');
}
if (!$sex) {
responseError('性别不存在');
}
if ($age < 1) {
responseError('年龄不可小于1');
}
if (!$description) {
responseError('描述不可为空');
}
$model = new demoModel();
$result = $model->insertInfo([
'name' => $name,
'sex' => $sex,
'age' => $age,
'description' => $description
]);
if ($result) {
responseSuccess([]);
}
responseError('添加失败');
}
}
3、demoModel.php
<?php
/**
* demoModel
*/
class demoModel
{
protected $_connect;
protected $_table = 'db_demo';
function __construct()
{
$config = [
'host' => '127.0.0.1',
'account' => 'root',
'password' => '021104',
'dbName' => 'test',
'charset' => 'utf8'
];
$this->_connect = new mysqli($config['host'], $config['account'], $config['password'], $config['dbName']);
if ($this->_connect->connect_error) {
dd('数据库链接失败');
}
$this->_connect->set_charset($config['charset']);
}
public function getName($id)
{
$sql = sprintf('SELECT * FROM `%s` WHERE `id` = %d', $this->_table, $id);
$res = $this->_connect->query($sql);
if (!$res) {
return '';
}
$result = [];
while ($row = $res->fetch_object()) {
$result[] = $row;
}
$row = reset($result);
return $row->name;
}
public function getList()
{
$sql = sprintf('SELECT * FROM `%s`', $this->_table);
$res = $this->_connect->query($sql);
if (!$res) {
return '';
}
$result = [];
while ($row = $res->fetch_object()) {
$result[] = $row;
}
return $result;
}
public function getUser($id)
{
$sql = sprintf('SELECT * FROM `%s` WHERE `id` = %d', $this->_table, $id);
$res = $this->_connect->query($sql);
if (!$res) {
return '';
}
$result = [];
while ($row = $res->fetch_object()) {
$result[] = $row;
}
return reset($result);
}
public function update($id, array $params)
{
$sql = sprintf(
'UPDATE `%s` SET `name` = "%s", `sex` = "%s", `age` = %d, `description` = "%s" WHERE `id` = %d',
$this->_table,
$params['name'],
$params['sex'],
$params['age'],
$params['description'],
$id
);
$res = $this->_connect->query($sql);
return $res !== false;
}
public function insertInfo(array $params){
$sql = sprintf('INSERT INTO `%s` (`name`,`sex`,`age`,`description`) VALUES ("%s","%s",%d,"%s")',
$this->_table,
$params['name'],
$params['sex'],
$params['age'],
$params['description']
);
$res = $this->_connect->query($sql);
return $res !== false;
}
public function remove($id)
{
$sql = sprintf('DELETE FROM `%s` WHERE `id` = %d', $this->_table, $id);
$res = $this->_connect->query($sql);
return $res !== false;
}
}
controller层负责拿数据,只关心结果,model层是具体操作数据库的。
目录结构如下:
add.html
edit.html
参数params(将form的name作为键,value作为值)中用了$.post,请求到了.php文件中去,即可用超级全局变量$_POST来调用