Understanding MVC programming in PHP - Introduction to the MVC framework

巴扎黑
Release: 2023-03-02 17:38:01
Original
1232 people have browsed it

【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.

Related labels:
mvc
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template