php实现mvc框架
PHP 实现 MVC 框架
MVC(Model-View-Controller)是一种常见的软件设计模式,用于分离业务逻辑、数据和用户界面。以下是使用 PHP 实现 MVC 框架的关键步骤。
目录结构
一个基本的 MVC 框架目录结构如下:
app/
controllers/
models/
views/
public/
index.php
入口文件
public/index.php 是框架的入口文件,负责初始化框架并处理请求。
<?php
require_once '../app/bootstrap.php';
$router = new Router();
$router->dispatch();
路由处理
路由负责解析 URL 并调用相应的控制器和方法。
class Router {
public function dispatch() {
$url = isset($_GET['url']) ? $_GET['url'] : 'home/index';
$url = explode('/', $url);
$controllerName = ucfirst($url[0]) . 'Controller';
$methodName = isset($url[1]) ? $url[1] : 'index';
require_once '../app/controllers/' . $controllerName . '.php';
$controller = new $controllerName();
$controller->$methodName();
}
}
控制器
控制器负责处理业务逻辑并调用模型和视图。
class HomeController {
public function index() {
$model = new HomeModel();
$data = $model->getData();
$view = new View('home/index', $data);
$view->render();
}
}
模型
模型负责处理数据逻辑,通常与数据库交互。
class HomeModel {
public function getData() {
return ['title' => 'Welcome to MVC Framework'];
}
}
视图
视图负责展示数据,通常是一个 HTML 模板。
class View {
private $template;
private $data;
public function __construct($template, $data) {
$this->template = '../app/views/' . $template . '.php';
$this->data = $data;
}
public function render() {
extract($this->data);
require_once $this->template;
}
}
视图模板
视图模板文件 app/views/home/index.php 可能如下:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
</body>
</html>
自动加载
使用 spl_autoload_register 自动加载类文件,避免手动引入。
spl_autoload_register(function ($className) {
$file = '../app/' . str_replace('\\', '/', $className) . '.php';
if (file_exists($file)) {
require_once $file;
}
});
数据库连接
模型通常需要与数据库交互,可以在框架中集成数据库连接。
class Database {
private $connection;
public function __construct() {
$this->connection = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
}
public function query($sql, $params = []) {
$stmt = $this->connection->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
配置管理
使用配置文件管理数据库连接等设置。
class Config {
public static function get($key) {
$config = [
'db_host' => 'localhost',
'db_name' => 'test',
'db_user' => 'username',
'db_pass' => 'password'
];
return isset($config[$key]) ? $config[$key] : null;
}
}
错误处理
自定义错误处理以提高框架的健壮性。
set_exception_handler(function ($exception) {
echo 'Error: ' . $exception->getMessage();
});
示例使用
以下是一个完整的示例,展示如何使用该框架。
- 创建控制器
app/controllers/HomeController.php:
class HomeController {
public function index() {
$model = new HomeModel();
$data = $model->getData();
$view = new View('home/index', $data);
$view->render();
}
}
- 创建模型
app/models/HomeModel.php:
class HomeModel {
public function getData() {
return ['title' => 'Welcome to MVC Framework'];
}
}
- 创建视图
app/views/home/index.php:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
</body>
</html>
- 访问
http://localhost/public/index.php?url=home/index即可看到结果。
扩展功能
可以根据需要扩展框架功能,例如:

- 添加中间件支持
- 集成模板引擎
- 支持 RESTful API
- 添加缓存机制
通过以上步骤,可以构建一个基础的 PHP MVC 框架,并根据需求进一步扩展。






