当前位置:首页 > PHP

php mvc的实现

2026-03-13 08:05:59PHP

PHP MVC 实现方法

MVC(Model-View-Controller) 是一种设计模式,用于分离应用程序的业务逻辑、用户界面和用户输入。以下是 PHP 中实现 MVC 的基本方法。

目录结构

创建以下目录结构来组织代码:

  • app/
    • controllers/ 存放控制器
    • models/ 存放模型
    • views/ 存放视图
  • public/ 存放入口文件和静态资源
  • config/ 存放配置文件

模型(Model)

模型负责处理数据和业务逻辑。创建一个基础模型类,其他模型可以继承它。

// app/models/Model.php
class Model {
    protected $db;

    public function __construct() {
        $this->db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
    }
}

// app/models/User.php
class User extends Model {
    public function getUsers() {
        $stmt = $this->db->query("SELECT * FROM users");
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}

视图(View)

视图负责显示数据。通常使用简单的 PHP 文件或模板引擎。

// app/views/users/index.php
<!DOCTYPE html>
<html>
<head>
    <title>Users</title>
</head>
<body>
    <h1>Users</h1>
    <ul>
        <?php foreach ($users as $user): ?>
            <li><?php echo $user['name']; ?></li>
        <?php endforeach; ?>
    </ul>
</body>
</html>

控制器(Controller)

控制器处理用户请求,调用模型获取数据,并传递给视图。

// app/controllers/UserController.php
class UserController {
    public function index() {
        $userModel = new User();
        $users = $userModel->getUsers();
        require 'app/views/users/index.php';
    }
}

路由

路由将 URL 映射到相应的控制器和方法。

// public/index.php
require '../app/controllers/UserController.php';

$action = $_GET['action'] ?? 'index';
$controller = $_GET['controller'] ?? 'user';

$controllerClass = ucfirst($controller) . 'Controller';
if (class_exists($controllerClass)) {
    $controllerInstance = new $controllerClass();
    if (method_exists($controllerInstance, $action)) {
        $controllerInstance->$action();
    } else {
        echo "Action not found";
    }
} else {
    echo "Controller not found";
}

自动加载

使用自动加载来避免手动包含文件。

php mvc的实现

// public/index.php
spl_autoload_register(function ($class) {
    $file = '../app/' . str_replace('\\', '/', $class) . '.php';
    if (file_exists($file)) {
        require $file;
    }
});

$action = $_GET['action'] ?? 'index';
$controller = $_GET['controller'] ?? 'user';

$controllerClass = 'controllers\\' . ucfirst($controller) . 'Controller';
if (class_exists($controllerClass)) {
    $controllerInstance = new $controllerClass();
    if (method_exists($controllerInstance, $action)) {
        $controllerInstance->$action();
    } else {
        echo "Action not found";
    }
} else {
    echo "Controller not found";
}

使用框架

对于更复杂的应用,可以考虑使用现有的 PHP MVC 框架,如 Laravel、Symfony 或 CodeIgniter。这些框架提供了更多的功能和更好的结构。

标签: phpmvc
分享给朋友:

相关文章

php实现搜索功能

php实现搜索功能

实现基础搜索功能 使用PHP和MySQL实现基础的搜索功能需要结合表单提交与数据库查询。创建一个HTML表单用于接收用户输入的搜索关键词,通过PHP处理表单数据并查询数据库。 // 搜索表单 (HT…

php长连接实现

php长连接实现

PHP 长连接实现方法 长连接(Keep-Alive)允许客户端与服务器在一次TCP连接中发送和接收多个HTTP请求/响应,减少重复建立连接的开销。以下是PHP中实现长连接的几种方式: 配置HTTP…

php实现上传

php实现上传

PHP 文件上传实现 HTML 表单设置 创建一个包含文件上传字段的 HTML 表单,确保表单的 enctype 属性设置为 multipart/form-data,这是文件上传必需的设置。 <…

php 购物车实现

php 购物车实现

数据库设计 购物车功能通常需要设计数据库表存储商品和用户信息。常见的表包括products(商品表)、users(用户表)和cart(购物车表)。cart表通常包含字段:id(主键)、user_id(…

php实现mysql

php实现mysql

PHP 连接 MySQL 的基本方法 PHP 提供了多种方式与 MySQL 数据库进行交互,以下是最常用的几种方法。 使用 MySQLi 扩展 MySQLi 是 MySQL 的改进扩展,支持面向对…

php 实现跳转

php 实现跳转

PHP 实现页面跳转的方法 在PHP中,实现页面跳转可以通过多种方式完成,以下是几种常用的方法: 使用header函数 header函数是PHP中最常用的跳转方法,通过发送HTTP头部信息实现跳转。…