search
HomeBackend DevelopmentPHP Tutorial[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工作流程中的第一步调用控制器,并对它发出指令

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)