Understanding MVC framework programming in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags ldap ruby on rails
Understand the MVC framework programming in PHP. What is MVCMVC is a concept that allows you to combine the "three parts (namely, the full name of MVC, Model, View, and Controller)" into a complex application. A car is What is MVC?

MVC is a concept that allows you to combine the "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. We can View both the internal and external views. The two cannot be separated from one Controller: the driver. The brake system, steering wheel, and other control systems represent the Model: they obtain control methods from the Controller and apply them to the internal and external View ).

MVC on the network

The MVC framework covers a simple and extremely flexible concept. The basic concept is that you have a separate controller (such as index. php) to control all the applications built on the parameter request framework. This controller usually contains (to a minimum) a parameter defining the model, an event, and a GET parameter. In this way, the controller can confirm all requests and then run corresponding events. For example, what is/index. php like this? Module = foo & event = bar is probably used to load a class named foo, and then run foo: bar () [is the bar () function]. The benefits of doing so are:

An interface corresponding to all applications

At the same time, it is very troublesome to maintain countless codes in an application, because each piece of code has its own relative path, database link, verification, and so on. In this way, you will be relieved of your troubles in this regard, allowing you to merge and reuse the code.

Why create your own MVC framework?

So far, I have never seen many MVC frameworks written in PHP. In fact, I only know one-Solar, which is completely written in PHP5. The other is Cake, a RoR that tries to become a PHP (Ruby on Rails-a Ruby open-source network framework ). I myself have some dissatisfaction with these two frameworks: they have not used the existing code contained in PEAR, Smarty, and so on; the current Cake is still disordered; finally, solar is a vast majority of works written by one person (I have no intention to say that Paul is not a good person or a good programmer ). These problems may not allow you to deny them, and you may not care about them at all. But because of this, I ask you to review them as much as possible.

Old mode

If you go back to check the code written by the author, you can find a file named template.txt, which looks like this:

<?php require_once('config.php'); // Other requires, DB info, etc. $APP_DB = 'mydb';$APP_REQUIRE_LOGIN = false; // Set to true if script requires login$APP_TEMPLATE_FILE = 'foo.php'; // Smarty template$APP_TITLE = "My Application"; 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());} // Put your logic here // Output the templateinclude_once(APP_TEMPLATE_PATH.'/header.php');include_once(APP_TEMPLATE_PATH.'/'.$APP_TEMPLATE_FILE);include_once(APP_TEMPLATE_PATH.'/footer.php'); ?>

But reading this code will make me have a desire to retreat. The concept of this code is to ensure that every application can apply to this processing method. for example, I can simply copy template.txt to myapp. php: change some variables. you can see that it can run. However, this well-organized solution has some serious disadvantages:

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

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

How can I handle various codes (including editing, upgrading, and deleting )?

How can I handle multi-level authentication (administrator vs. non-administrator )?

How can I enable the output cache?

New methods

By throwing everything into this MVC framework, you will find that life is so simple. Compare the following code:

<?php class myapp extends FR_Auth_User{public function __construct(){parent::__construct();} public function __default(){// Do something here} public function delete(){ } public function __destruct(){parent::__destruct();}} ?>

Note that this code is clearly not used to link to a database, determine whether a user has logged in, or output any other information. The controller knows everything.

If I want to verify LDAP, I can establish FR_Auth_LDAP. The controller can identify some output methods (such as $ _ GET ['output']) and convert them to PDF or SOAP at any time. Delete is only responsible for event processing, and does not care about others. Because this module has an FR_User class instance, it can easily determine whether a user has logged on.

Smarty is taken for granted as a template engine to control the cache, but the controller can also control a portion of the cache.

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


Using MVC is a concept that allows you to combine the "three parts (the full name of MVC, Model, View, and Controller)" into a complex application. A car is...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.