An introduction to the MVC Framework for Understanding MVC programming in PHP

Source: Internet
Author: User
Tags header soap ldap pear requires smarty template ruby on rails
Programming "What is MVC?" "

MVC is a concept that allows you to harmonize "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 look at the two view (show) section: internal and external. And these two are inseparable from a controller (Controller): the driver. braking systems, steering wheels and other control systems represent model: they take control from the driver (Controller) and apply it to the interior and appearance (View).

   "MVC on the Network"

The concepts covered by the MVC framework are fairly simple and extremely flexible. The basic concept is that you have a single controller (such as index.php) that controls all the applications built on the parameter request based on the framework. This controller usually contains (to a minimum extent) a definition of the model's parameters, an event, and a get parameter. This allows the controller to confirm all requests and then run the appropriate events. For example, a/index.php?module=foo&event=bar request like this might be used to load a class named Foo, and then run Foo::bar () [which is the bar () function]. The benefits of doing so are:

An interface that corresponds to all applications

Maintaining the myriad of code within an application is cumbersome, because each piece of code has its own relative path, database links, validation, and so on. And this eliminates your annoyance in this area, allowing you to merge and reuse the code

   "Why create the author's own MVC framework?" "

So far, I haven't seen much of the MVC framework written in PHP. In fact, I just know that a-solar is written entirely in PHP5. The other is cake, a ror (Ruby on rails-a ruby language Open Source network framework) that tries to become PHP. I myself have some dissatisfaction with both of these frameworks: none of them use the existing code contained in the Pear,smarty, etc.; now the cake is still quite disordered; Finally, Solar is a piece of work written by one person (I don't mean to say that the author Paul is not a good or a good programmer). These questions may not make you deny them, and chances are you don't care about them at all. But that's why I ask you to look at them as much as possible.

   "The old Way"

If you go back to 2001 to see the code you wrote, the author may find a file called Template.txt, it looks like this: www.phpv.net reprint please indicate the source

<?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
Output the template

Include_once (App_template_path. ') /header.php ');
Include_once (App_template_path. ') /'. $APP _template_file);
Include_once (App_template_path. ') /footer.php ');
? >
Oh my God, just looking at these codes will give me the urge to shrink. The concept of this code is to make sure that every application applies to this process, such as I can simply copy template.txt into the myapp.php, change some variables, and see, it can run. Still, there are some serious drawbacks to this well-organized approach:

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

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

How do I handle different kinds of code (including editing, upgrades, and deletions)?

How do I handle multilevel authentication (administrator vs. non-admin)?
How do I enable output caching? Www.phpv.net Reprint please indicate the source

   "New Way"

Throw everything into this MVC framework and you'll find life is so simple. Please 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, to determine whether a user has logged in, or to output any other information. The controller has mastered everything.

If I want to verify LDAP, I can build 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. Event handling Delete, which is only responsible for deletion, the rest of it does not matter. Because this module has an instance of a Fr_user class, it can simply determine whether a user has landed and so on. Smarty, the template engine controls caching as a matter of course, but the controller can also control part of the cache.

The old way from the previous to the MVC approach may be a new and unfamiliar concept for many people, but once you switch to such a concept, it will be quite difficult to turn back.

[1] [2] [3] [4] [5] Next page



Related Article

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.