mvc 小案例
mvc 框架
index 控制器
<?php
namespace zcgl;
session_start();
//session_destroy() ;
//session_id() ;
//exit();
if (isset($_SESSION['name'])) {
echo '用户: ' . $_SESSION['name'] . '已登录<br>';
echo '<a href="dispatch.php?action=logout">退出</a>';
} else {
// 2. 未登录,就跳转到登录页面
echo '<a href="dispatch.php?action=login">请登录</a>';
}
login 登录界面
`<?php
// 防止用户重复登录
if (isset($_SESSION[‘name’])) {
echo ‘<script>alert(“不要重复登录”);location.assign(“/sbgl/zcgl/index.php”);</script>‘;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>设备管理库</title>
<link rel="stylesheet" href="/sbgl/zcgl/statuc/css/login.css">
</head>
<body>
<div class="container">
<h3>用户登陆</h3>
<form action="/sbgl/zcgl/dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
<div>
<label for="user">用户名:</label>
<input type="text" name="user" id="user">
</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" id="password">
</div>
<div>
<button>提交</button>
</div>
</form>
</div>
</body>
<script>
function isEmpty() {
var user = document.getElementById(‘user’).value;
var password = document.getElementById(‘password’).value;
if (user.length=== 0 || password.length===0) {
alert(‘手机和密码不能为空’);
return false;
}
}
</script>
</html>`
验证进入后台主主菜单
usermodel
`<?php
namespace model;
use PDO;
//数据连接,数据查询
class Db
{
//数据库连接
private $dsn;
private $user;
private $password;
private $pdo;
public function connect()
{
try {
$this->pdo = new PDO($this->dsn, $this->user, $this->password);
} catch (PDOException $e) {
die('数据库连接失败,错误信息:' . $e->getMessage());
}
}
public function __construct($dsn = 'mysql:host=localhost;dbname=sbgl', $user = 'root', $password = 'root')
{
// $this->pdo = new PDO($dsn,$user,$password);
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
$this->connect();
}
//获取用户全部信息
public function seleUsers()
{
$sql = 'SELECT * FROM `users`';
$stmt = $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// 查询多条记function select($table,$fields='*',$where='',$order='',$limit='// 创建SQL语句
// 查询多条记录
function select($table, $fields = ‘*’, $where = ‘’, $order = ‘’, $limit = ‘’)
{
// 创建SQL语句
$sql = ‘SELECT ‘;
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field . ‘, ‘;
}
} else {
$sql .= $fields;
}
$sql = rtrim(trim($sql), ‘,’);
$sql .= ‘ FROM ‘ . $table;
// 查询条件
if (!empty($where)) {
$sql .= ‘ WHERE ‘ . $where;
}
// 排序方式
if (!empty($order)) {
$sql .= ‘ ORDER BY ‘ . $order;
}
// 分页
if (!empty($limit)) {
$sql .= ‘ LIMIT ‘ . $limit;
}
$sql .= ‘;’;
// 创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
// 执行查询操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetchAll();
}
}
return ‘查询失败’;
}
// 查询单条记录
function find($table,$fields,$where=’’){
$sql = ‘SELECT ‘;
if(is_array($fields)){
foreach ($fields as $field){
$sql .= $field . ‘, ‘;
}
}else{
$sql .= $fields;
}
$sql = rtrim(trim($sql),’,’);
$sql .= ‘ FROM ‘ . $table;
if(!empty($where)){
$sql .= ‘ WHERE ‘ . $where;
}
$sql .= ‘ LIMIT 1;’;
$stmt = $this->pdo->prepare($sql);
// print_r($stmt);
if($stmt->execute()){
if ($stmt->rowCount()>0){
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetch();
}
}
return ‘查询失败’;
}
}
//
// $db = new Db();
//print_r($db->seleUsers());
//$data =$db->select(‘users’);
////print_r($data);
//查询多条记录
// echo ‘<pre>‘ .print_r($db->select(‘users’),true);
// 查询单条记录
//echo ‘<pre>‘ .print_r($db->find(‘users’,’*’,’user = “gzg”‘ ),true);
//$data =new Db();
//$data1=$data->find(‘users’,’*’,’user=”gzg”‘);
//print_r($data1);
‘
控制器
‘<?php
namespace zcgl;
use model\Db;
use view\View;
require DIR . ‘/Model/usersModel.php’;
require DIR . ‘/view/userView.php’;
class Controller1
{
public function index()
{
// 3.1 获取数据
// $model = new UserModel();
// $data = $model->getData();
$data1 =new Db();
$data=$data1->select(‘users’);
// print_r($data) ;
// 3.2 渲染模板
$view = new View();
return $view->fetch($data);
}
}
$controller = new Controller1();
echo $controller->index();`
‘’
useView 视图
`<?php
namespace view;
// 视图类:渲染数据
class View
{
public function fetch($data)
{
// exit;
$table = '<table>';
$table .= '<caption>用户信息表</caption>';
$table .= '<tr><th>ID</th><th>用户</th><th>手机号</th><th>密码</th></tr>';
foreach ($data as $product) {
$table .= '<tr>';
$table .= '<td>' . $product['id'] . '</td>';
$table .= '<td>' . $product['user'] . '</td>';
$table .= '<td>' . $product['phone'] . '</td>';
$table .= '<td>' . $product['password'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
echo ‘<style>
table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}
caption {font-size: 1.2rem; margin-bottom: 10px;}
tr:first-of-type { background-color:lightblue;}
td,th {border: 1px solid}
td:first-of-type {text-align: center}
</style>‘;`
logout 退出
<?php
//已经登录的情况下再退出
if (isset($_SESSION['name'])) {
setcookie('name', null, time()-3600);
echo '<script>alert("退出成功");location.assign("/sbgl/zcgl/view/login.php");</script>';
} else {
// 要求用户先登录
echo '<script>alert("请先登录");location.assign("/sbgl/zcgl/view/login.php");</script>';
}
总结
对类 的调用和mvc 有一定的了解,近期抽空把 此程序中的 usercont.php 用容器和接口再写一遍,把数据库删除和修改添加上去。
主要问题好像每个模块都懂但是串联时还是很吃力,估计还是练得少造成的。