Home Backend Development PHP Tutorial Understanding MVC programming in PHP - Introduction to the MVC framework

Understanding MVC programming in PHP - Introduction to the MVC framework

Dec 01, 2016 am 11:11 AM
mvc

【What is MVC? ]

MVC is a concept that allows you to harmoniously combine "three parts (the full name of MVC, Model, View, Controller)" into a complex application. A car is a very good example of MVC in real life. When we look at a car, we look at two View (display) parts: interior and exterior. Both of these are inseparable from a Controller: the driver. The brake system, steering wheel, and other control systems represent the Model: they take the control methods from the driver (Controller) and apply them to the interior and exterior (View).

【MVC on the Web】

The concepts covered by the MVC framework are quite simple and extremely flexible. The basic concept is that you have a single controller (such as index.php) that controls all applications within the framework that are based on parameter requests. This controller usually contains (minimally) a parameter that defines the model, an event, and a GET parameter. This way the controller can acknowledge all requests and run the appropriate events. For example, a request like this /index.php?module=foo&event=bar is probably used to load a class named foo, and then run foo::bar()[which is bar( )function]. The benefits of this are:

An interface corresponding to all applications

It is very troublesome to maintain countless codes in an application at the same time, because each piece of code has its own relative path, database link, verification, etc. Doing so will save you the trouble in this regard and allow you to merge and reuse code

【Why create the author's own MVC framework? ]

So far, I have not seen too many MVC frameworks written in PHP. In fact, I only know of one - Solar, which is completely written in PHP5. The other one is Cake, a RoR that tries to be PHP (Rubyalign=centerbgColor=#e3e3e3border=1>

<?php 
 require_once('config.php');//Otherrequires,DBinfo,etc. 
 $APP_DB='mydb'; 
 $APP_REQUIRE_LOGIN=false;//Settotrueifscriptrequireslogin 
 $APP_TEMPLATE_FILE='foo.php';//Smartytemplate 
 $APP_TITLE='MyApplication'; 
 if($APP_REQUIRE_LOGIN==true){ 
if(!isset($_SESSION['userID'])){ 
 header("Location:/path/to/login.php"); 
 exit(); 
} 
 } 
 $db=DB::connect('mysql://'.$DB_USER.':'.$DB_PASS.'@localhost/'.$APP_DB); 
 if(!PEAR::isError($db)){ 
$db->setFetchMode(DB_FETCHMODE_ASSOC); 
 }else{ 
die($db->getMessage()); 
 } 
 //Putyourlogichere 
 //Outputthetemplate 
 include_once(APP_TEMPLATE_PATH.'/header.php'); 
 include_once(APP_TEMPLATE_PATH.'/'.$APP_TEMPLATE_FILE); 
 include_once(APP_TEMPLATE_PATH.'/footer.php'); 
?>
Copy after login


God, just looking at this code makes me cringe. The concept of this code is to ensure that every application Any program can be adapted to this approach, for example I can simply copy template.txt into myapp.php, change a few variables, and voila, it works. However, there are some serious drawbacks to this well-organized approach. Disadvantages:

What should I do if my boss wants the author to use myapp.php to output PDF in some cases, HTML in some cases, and SOAP in some cases (directly submitted XML request)?

What if this application? Program requires IMAP or LDAP authentication, what should I do?

How do I handle various different codes (including edits, upgrades, and deletes)?

How do I handle multiple levels of authentication (admin vs. non-admin)? ?
How do I enable output caching? www.phpv.net Please indicate the source for reprinting

[New way]

Put everything into this MVC framework and you will find that life is so simple: Please compare the following code:

<?php 
 classmyappextendsFR_Auth_User 
 { 
publicfunction__construct() 
{ 
 parent::__construct(); 
} 
 publicfunction__default() 
 { 
//Dosomethinghere 
 } 
 publicfunctiondelete() 
 {} 
 publicfunction__destruct() 
 { 
parent::__destruct(); 
 } 
} 
?>
Copy after login

Note that this code is obviously not used to link to a database, determine if a user is logged in, or output any other information.

If I want to authenticate to LDAP, I can set it up. FR_Auth_LDAP. The controller can recognize certain output methods (such as $_GET['output']) and can be converted to PDF or SOAP at any time. The event handler is only responsible for deletion, because this module has a FR_User class. For example, it can simply determine whether a user has logged in, etc. Smarty, as a template engine, controls the cache, but the controller can also control part of the cache.

From the old method mentioned above to the MVC method, it is for many people. It may be a new and unfamiliar concept, but once you switch to such a concept, it will be quite difficult to switch back.

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP MVC Architecture: Building Web Applications for the Future PHP MVC Architecture: Building Web Applications for the Future Mar 03, 2024 am 09:01 AM

Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

An advanced guide to PHP MVC architecture: unlocking advanced features An advanced guide to PHP MVC architecture: unlocking advanced features Mar 03, 2024 am 09:23 AM

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection

How to implement the MVC pattern using PHP How to implement the MVC pattern using PHP Jun 07, 2023 pm 03:40 PM

The MVC (Model-View-Controller) pattern is a commonly used software design pattern that can help developers better organize and manage code. The MVC pattern divides the application into three parts: Model, View and Controller, each part has its own role and responsibilities. In this article, we will discuss how to implement the MVC pattern using PHP. Model A model represents an application's data and data processing. usually,

Uncovering the success of the SpringMVC framework: why it is so popular Uncovering the success of the SpringMVC framework: why it is so popular Jan 24, 2024 am 08:39 AM

SpringMVC framework decrypted: Why is it so popular, specific code examples are needed Introduction: In today's software development field, the SpringMVC framework has become a very popular choice among developers. It is a Web framework based on the MVC architecture pattern, providing a flexible, lightweight, and efficient development method. This article will delve into the charm of the SpringMVC framework and demonstrate its power through specific code examples. 1. Advantages of SpringMVC framework Flexible configuration method Spr

How to use MVC architecture to design projects in PHP How to use MVC architecture to design projects in PHP Jun 27, 2023 pm 12:18 PM

In Web development, MVC (Model-View-Controller) is a commonly used architectural pattern for processing and managing an application's data, user interface, and control logic. As a popular web development language, PHP can also use the MVC architecture to design and build web applications. This article will introduce how to use MVC architecture to design projects in PHP, and explain its advantages and precautions. What is MVCMVC is a software architecture pattern commonly used in web applications. MV

Developing MVC with PHP8 Framework: A Step-by-Step Guide Developing MVC with PHP8 Framework: A Step-by-Step Guide Sep 11, 2023 am 10:05 AM

Developing MVC with PHP8 Framework: A Step-by-Step Guide Introduction: MVC (Model-View-Controller) is a commonly used software architecture pattern that is used to separate the logic, data and user interface of an application. It provides a structure that separates the application into three distinct components for better management and maintenance of the code. In this article, we will explore how to use the PHP8 framework to develop an application that conforms to the MVC pattern. Step One: Understand the MVC Pattern Before starting to develop an MVC application, I

From routing to views—an in-depth exploration of Beego's MVC architecture From routing to views—an in-depth exploration of Beego's MVC architecture Jun 23, 2023 am 10:53 AM

Beego is a web application framework based on Go language, which has the advantages of high performance, simplicity and ease of use, and high scalability. Among them, the MVC architecture is one of the core design concepts of the Beego framework. It can help developers better manage and organize code, improve development efficiency and code quality. This article will delve into Beego's MVC architecture so that developers can better understand and use the Beego framework. 1. Introduction to MVC architecture MVC, or Model-View-Controller, is a common

Developing MVC with PHP8 framework: Important concepts and techniques that beginners need to know Developing MVC with PHP8 framework: Important concepts and techniques that beginners need to know Sep 11, 2023 am 09:43 AM

Developing MVC with PHP8 framework: Important concepts and techniques that beginners need to know Introduction: With the rapid development of the Internet, Web development plays an important role in today's software development industry. PHP is widely used for web development, and there are many mature frameworks that help developers build applications more efficiently. Among them, the MVC (Model-View-Controller) architecture is one of the most common and widely used patterns. This article will introduce how beginners can use the PHP8 framework to develop MVC applications.

See all articles