[PHP]MVC架构模式分析与设计
前言:
文章将介绍通过自己搭建一个MVC微型框架来了解MVC(model-view-control)三层的架构以及关系
MVC(Model-View-Control)三层介绍
为了更好地将各个功能层区分开发以便于不同专长的人各司其职,于是在众多开发者的努力下,以MVC为架构模式的框架越来越多,国内著名的PHP框架有Yii2,Yii以及ThinkPHP等国外的著名PHP框架Laravel等这些框架都是基于MVC模式开发的
简单地说 View是表现层,诸如一些html格式,xml格式等都可以充当该表现层的模板,Model是模型层,诸如一些具体的业务逻辑等都是在model中完成的。而Controller是控制器层,由它进行选择哪个模型以及哪个表现层,打个比方,控制器就像是遥控器而View就像是电视屏幕,而Model就像是电视节目,当然了,比方有些不太合适的地方
MVC工作流程
第一步 浏览者 -> 调用控制器,对它发出指令
第二步 控制器 -> 按指令选取一个合适的模型
第三步 模型 -> 按控制器指令取相应数据
第四步 控制器 -> 按指令选取相应的视图
第五步 视图 -> 把第三步取到的数据按用户想要的样子显示出来
该微型MVC框架组建简介
整个MVC包括两个文件夹和两个文件,分别是一个类库(libs),一个函数库(function),一个启动引擎(pc.php)以及一个require文件
该微型MVC框架的具体实现
function文件夹
该文件夹下存放了一些函数,这些函数的规模不能直接组成一个类库,只能封装成函数,不能封装成类。例如包括了一些简单调用及实例化控制器,模型,视图 的方法 (区别于Mysql类 因为 Mysql对应了一系列操作故能封装成类 )
注意在写这三个的方法时,控制器方法可以有两个参数,模型和视图对应的只能有一个参数 坑处
Mysql一系列操作之所以能封装成一个类,因为他们的一系列操作对应的是一个对象,而把 简单调用及实例化控制器,模型,视图的方法整合在一起写成一个类的话,他们的对象不能唯一,所以不能把 简单调用及实例化控制器,模型,视图的方法整合在一起写成一个类
libs文件夹
这个微型框架的库文件中包括了
-
核心库
能够直接调用,必然要用于实际开发的类文件例如:
1.数据库类(注意这里的数据库类和数据库库是不同的,数据库库里面存储的是具体的某一种数据库的具体操作方法,而数据库类的两个参数之一就是数据库类型也就是数据库库中的其中一个,简而言之就是用数据库类来操作具体的数据库)
2.视图类(这个类是用来初始化和调用View层的对象,在这里的这个框架中应该传入给这个类初始化方法的参数的是Smaty和Smarty的配置文件)
-
数据库库
用于存储各种数据库如mysql,mysqli,pdo等一系列具体操作(指的是增删改查)的代码
-
视图库
这里直接使用了Smarty第三方类库,Smarty是一个视图引擎,引擎具体体现在Smarty中的display方法,可以将模板文件(也可以是html文件)进行编译将其在视图层显示出来
include.list.php
pc.php
pc.php是一个启动引擎程序,这个程序中分别对数据库类,视图类进行了初始化,并且对controller和method静态变量进行了初始化,通过这两个静态变量实例化了一个控制器并调用了控制器,相当于MVC工作流程中的第一步调用控制器,并对它发出指令

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
