PHP搭建MVC框架基础入门

在Web开发领域,MVC(Model-View-Controller)架构因其清晰的职责划分和高效的代码组织方式,备受开发者青睐。本文将带你一步步搭建一个简单的PHP MVC框架,帮助你理解其核心概念和工作原理。

PHP搭建MVC框架基础入门

一、MVC架构简介

MVC是一种设计模式,将应用程序分为三个主要部分:

  • Model(模型):负责数据和业务逻辑。
  • View(视图):负责展示数据。
  • Controller(控制器):负责接收用户请求,调用模型处理数据,并选择视图进行展示。

这种分离使得代码更易于维护和扩展。

二、环境准备

在开始之前,确保你的开发环境已安装PHP和Web服务器(如Apache或Nginx)。创建一个项目目录,例如my_mvc_framework。

三、目录结构

一个基本的MVC框架目录结构如下:

my_mvc_framework/
├── app/
│   ├── controllers/
│   ├── models/
│   ├── views/
│   └── core/
│       ├── Controller.php
│       ├── Model.php
│       └── Router.php
├── public/
│   └── index.php
└── config/
    └── config.php

四、入口文件

在public目录下创建index.php,这是框架的入口文件:

<?php
require_once '../app/core/Router.php';
require_once '../app/core/Controller.php';
require_once '../app/core/Model.php';
require_once '../config/config.php';

$router = new Router();
$router->load();

五、路由器

在app/core/Router.php中定义路由器类:

<?php
class Router {
    public function load() {
        $url = $_GET['url'] ?? 'home';
        $url = rtrim($url, '/');
        $url = explode('/', $url);

        $controllerName = ucfirst($url[0]) . 'Controller';
        $controllerFile = '../app/controllers/' . $controllerName . '.php';

        if (file_exists($controllerFile)) {
            require_once $controllerFile;
            $controller = new $controllerName();
            $controller->index();
        } else {
            echo "Controller not found";
        }
    }
}

六、控制器基类

在app/core/Controller.php中定义控制器基类:

<?php
class Controller {
    protected function view($viewName, $data = []) {
        require_once '../app/views/' . $viewName . '.php';
    }

    protected function model($modelName) {
        require_once '../app/models/' . $modelName . '.php';
        return new $modelName();
    }
}

七、模型基类

在app/core/Model.php中定义模型基类:

<?php
class Model {
    protected $db;

    public function __construct() {
        $config = require_once '../config/config.php';
        $this->db = new PDO($config['db']['dsn'], $config['db']['user'], $config['db']['password']);
    }
}

八、配置文件

在config/config.php中定义数据库配置:

<?php
return [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=mydatabase',
        'user' => 'root',
        'password' => 'password'
    ]
];

九、示例控制器和视图

创建一个HomeController和一个对应的视图:

HomeController.php:

<?php
require_once '../core/Controller.php';

class HomeController extends Controller {
    public function index() {
        $this->view('home');
    }
}

home.php(视图):

<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
</head>
<body>
    <h1>Welcome to My MVC Framework</h1>
</body>
</html>

十、测试框架

在浏览器中访问http://localhost/my_mvc_framework/public/index.php?url=home,如果一切设置正确,你应该能看到“Welcome to My MVC Framework”的页面。

通过以上步骤,我们成功搭建了一个简单的PHP MVC框架。虽然这个框架功能有限,但它为你理解MVC架构和PHP框架的内部工作机制打下了基础。你可以在此基础上继续扩展,添加更多的功能和优化。希望这篇文章能激发你对PHP框架开发的兴趣!

你可能想看:
« 上一篇
下一篇 »

相关推荐

php实时推送,php实地培训

2026年10月05日

59阅读

php乱码出现问号的原因,php乱码出现问号的原因有哪些

2026年10月05日

52阅读

php一句话木马,一句话木马ctf

2026年10月05日

53阅读

php获取数组长度,php中如何获取数组的长度

2026年10月05日

59阅读

php面试题库及答案,php程序员面试真题库

2026年10月05日

55阅读

phpcontent-type修改,php源码二次修改需要多少钱

2026年10月05日

49阅读